APAddressBook icon indicating copy to clipboard operation
APAddressBook copied to clipboard

Duplicate contacts

Open nelsongc opened this issue 9 years ago • 19 comments

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?

nelsongc avatar Dec 09 '14 18:12 nelsongc

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?

callbacknull avatar Dec 10 '14 00:12 callbacknull

the list of contacts is right, but after this instruction

self.memoryStorage().addItems(contacts)

it appears duplicate contacts in the tableview

nelsongc avatar Dec 10 '14 01:12 nelsongc

It sounds like your issue is with DTTableViewManager then.

callbacknull avatar Dec 10 '14 01:12 callbacknull

the version of DTTableViewManager is (3.0.1), what version do I have to use?

nelsongc avatar Dec 10 '14 01:12 nelsongc

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

callbacknull avatar Dec 10 '14 02:12 callbacknull

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

funkyboy avatar Mar 18 '15 16:03 funkyboy

I was noticing the same issue. Due to linked contacts with iCloud

smasry avatar Mar 31 '15 02:03 smasry

I believe this issue is a duplicate of #18

emptyway avatar May 29 '15 03:05 emptyway

Please, provide the way to reproduce duplicated contact. What exactly, I should add in iphone's address book to have duplicated contacts?

belkevich avatar Jun 17 '15 15:06 belkevich

@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.

emptyway avatar Jun 17 '15 15:06 emptyway

Thank you very much! I'll check it

belkevich avatar Jun 17 '15 15:06 belkevich

So, what is the right behavior? What do you think?

belkevich avatar Jun 17 '15 16:06 belkevich

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.

emptyway avatar Jun 17 '15 17:06 emptyway

I'm having the same problem... No solution?

lteti77 avatar Jun 07 '16 18:06 lteti77

@lteti77

  1. is all contacts duplicated or just linked contacts?
  2. does Example app have same issue?
  3. please, share the code

belkevich avatar Jun 08 '16 12:06 belkevich

the linked contacts are duplicated. i haven't tested the example app, yet...

lteti77 avatar Jun 08 '16 12:06 lteti77

@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?

belkevich avatar Jun 09 '16 09:06 belkevich

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 avatar Jun 09 '16 11:06 lteti77

@lteti77 see this discussion. If you have solution, please let me know.

belkevich avatar Jun 09 '16 14:06 belkevich