I have an app where user can click on other user profile and than app will make comparison between phone contacts of both users and search for common contacts. That works, but it consumes much time so I was wondering is there a way I can speed up that at least a little bit.
****Note I'm beginner in iOS development and still not very familiar with Swift
syntax and iOS in general so any help would be very appreciated
Here is my simple Contact
class:
class Contact: Hashable, Equatable {
var hashValue: Int { get { return phoneNumber!.hashValue } }
var name: String?
var phoneNumber: String?
var thumbnail: Data?
var filled: Bool?
init() {
}
init(name: String?, phoneNumber: String?, thumbnail: Data?) {
self.name = name
self.phoneNumber = phoneNumber
self.thumbnail = thumbnail
}
static func ==(lhs: Contact, rhs: Contact) -> Bool {
return lhs.phoneNumber == rhs.phoneNumber
}
}
I have implemented, as you can see, Hashable
and Equatable
which I use to compare contacts by phone number and remove duplicates. Here is the code where I perform main operation of comparing. contacts
array contains phone contacts of user and otherContacts
is array of phone contacts of other user.
for result in self.contacts {
if self.otherContacts.contains(result){
self.commonContacts.append(result)
}
}
self.removedDuplicates = Array(Set(self.commonContacts))
if self.removedDuplicates.count == 1 {
self.commonFriends.text = "\(1) common friend"
}
else {
self.commonFriends.text = "\(self.removedDuplicates.count) common friends"
}
Thanks.