APAddressBook
APAddressBook copied to clipboard
Duplicate contacts
this is the code:
@IBOutlet weak var activity: UIActivityIndicatorView!
let addressBook = APAddressBook()
override func viewDidLoad() {
super.viewDidLoad()
self.registerCellClass(TableViewCell.self, forModelClass: APContact.self)
self.addressBook.fieldsMask = APContactField.FirstName | APContactField.LastName | APContactField.Emails
self.addressBook.sortDescriptors = [NSSortDescriptor(key: "firstName", ascending: true),
NSSortDescriptor(key: "lastName", ascending: true)]
self.addressBook.filterBlock = {(contact: APContact!) -> Bool in
println("\(contact.firstName), \(contact.lastName)")
return contact.emails != nil && contact.emails.count > 0
}
self.addressBook.loadContacts(
{ (contacts: [AnyObject]!, error: NSError!) in
self.activity.stopAnimating()
if (contacts != nil) {
self.memoryStorage().addItems(contacts)
}
else if (error != nil) {
let alert = UIAlertView(title: "Error", message: error.localizedDescription,
delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
})
}
the result is a list with all contacts duplicated, why? do I need something more?
That looks right - I haven't familiarized myself with swift but I literally just wrote the same thing in obj-c yesterday (I use DTTableManager too) with no issues.
Have you checked the contacts array in the debugger before you add it to the memoryStorage?
the list of contacts is right, but after this instruction
self.memoryStorage().addItems(contacts)
it appears duplicate contacts in the tableview
It sounds like your issue is with DTTableViewManager then.
the version of DTTableViewManager is (3.0.1), what version do I have to use?
What exactly is your cell? It sounds like you probably want a customized cell - but it looks like your registering UITableViewCell as your cell class?
For custom cells to work properly in DTTableViewManager you need to either subclass DTTableViewCell or add the DTModelTransfer protocol to your cell and implement the updateWithModel: method. Check out the Examples directory in the github repo
I am having the same issue, duplicated contacts. I suspect this is related to the fact that my contacts on the device are "linked". Those that appear twice are in fact linked to both iCloud and iPhone.
The array in loadContacts:
already returns duplications, so it's not an issue with the way I populate my table view.
I had to use an NSSet like this
NSMutableSet *tmp = [NSMutableSet set];
for (APContact *c in contacts) {
NSString *name = c.compositeName;
if (name == nil) {
name = @"";
}
for (NSString *s in c.phones) {
NSString *res = [s stripPhoneNumber];
ShareContact *c = [[ShareContact alloc] initWithName:name andPhone:res];
[tmp addObject:c];
}
}
and override both isEqual:
and hash
in my custom class to have a correct detection of uniques in the set.
HTH
I was noticing the same issue. Due to linked contacts with iCloud
I believe this issue is a duplicate of #18
Please, provide the way to reproduce duplicated contact. What exactly, I should add in iphone's address book to have duplicated contacts?
@belkevich Go to your contact list and Edit an existing contact. At the bottom you will see an option as + Link Contacts press it and link to another contact. After this operation, these two contacts are linked and merged, and you will see it as 1 contact on your phone book. But when fetching with APAddressBook you will see it as separated.
Thank you very much! I'll check it
So, what is the right behavior? What do you think?
IMHO APAddressBook should returns the same contact list as Contacts App. Any duplicated contact still can be retrieved with APContactFieldLinkedRecordIDs if you want to use it.
Since APContactFieldLinkedRecordIDs only retrieve the linked RecordIDs, a nice to have solution is to add a APContactFieldLinkedRecordContacts flag to pre-populate the linked APContacts into an array.
I'm having the same problem... No solution?
@lteti77
- is all contacts duplicated or just linked contacts?
- does
Example
app have same issue? - please, share the code
the linked contacts are duplicated. i haven't tested the example app, yet...
@lteti77
I've created 2 contacts aaa
and bbb
and linked bbb
into aaa
. So, I see only aaa
in Contacts.app
. But Example.app
shows me both aaa
and bbb
. Is that your problem?
yes, it is. In my code i fixed it by iterating the contatcts and checking if each contacts has some linked contacts. In that case, i remove them from the list.
@lteti77 see this discussion. If you have solution, please let me know.