1

I have written an app that gets access to addressbook, the user is supposed to be able to delete a contact from the app and it will delete that contact from the addressbook

Sometimes this just doesn't happen, although when tracing, everything works as expect and I get notified that ABAddressBookRegisterExternalChangeCallback(); right after the delete, but when I go to contact app on the phone, the total number of contacts remain the same and that contact is still there, not deleted.

Here is the code for deletion

// this method takes and NSManagedObject which holds the id of the person that should be deleted

- (void)deleteContactFromAddressBook:(NSManagedObject *)object
{
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    NSNumber *contactID = [object valueForKey:@"contactID"];
    ABRecordRef contactRef = ABAddressBookGetPersonWithRecordID(addressBook, [contactID intValue]);

    NSString *firstName = (__bridge NSString *)ABRecordCopyValue(contactRef,
                                                             kABPersonFirstNameProperty);
    NSString *lastName = (__bridge NSString *)ABRecordCopyValue(contactRef,
                                                            kABPersonLastNameProperty);
    if (!lastName) {
        lastName = @"";
    }
    NSLog(@"contact to be deleted - Name: %@" ,[firstName stringByAppendingString:lastName]);

    BOOL deleteCheck = ABAddressBookRemoveRecord(addressBook, contactRef, NULL);

    BOOL saveCheck = ABAddressBookSave(addressBook, NULL);

    NSLog(@"delete check = %hhd ---------- save check = %hhd", deleteCheck, saveCheck);
    NSLog(@"Contact Deleted with id: %@", contactID);
}

If you need further explanation don't hesitate to ask. I am really frustrated by this unexpected behavior and I have no clue where to look.

Thank you.

Moody
  • 352
  • 3
  • 15
  • Does the deletion not appear immediately, or is the record never deleted? I assume the log statement looks ok, even when it doesn't succeed? And `deleteCheck` and `saveCheck` are both OK? And do you have any other `ABAddressBookRef` objects banging about? Presumably unrelated, but you have three leaks here: replace those `__bridge` references with `__bridge_transfer` and don't forget to `CFRelease(addressBook)`. If you use static analyzer ("Analyze" on Xcode's "Product" menu), it will undoubtedly warn you about these. – Rob Sep 07 '14 at 21:19
  • BTW, if either `deleteCheck` or `saveCheck` are false, I'd suggest using the `CFErrorRef` parameter of those two functions. – Rob Sep 07 '14 at 21:31
  • @Rob The record is never deleted, and the `addressbook` total contacts remain the same, but i get the message delete, `deleteCheck` & `saveCheck` are = 1, so they are true, and i get notified that the `addressbook` has changed. Thanks for pointing out the `__bridge_transfer` and `CFRlease(addressbook)` I have never come to the situation that `delectCheck` or `saveCheck` are false. Not sure what is going wrong here. – Moody Sep 08 '14 at 00:49
  • The aforementioned leaks notwithstanding (which it sounds like you've addressed), this code looks fine. I've used it to delete contacts and the main contacts app reflects the deletions fine. So I have to suspect that the problem must rest elsewhere. I might suggest creating a minimalist demonstration project that manifests your problem, but in my test project, I was unable to reproduce the behavior you describe. – Rob Sep 08 '14 at 03:57
  • Ok, I am still having the same issue, but at least i understand now that calling `ABAdressBookSave(addressBook, NULL)` alone will fire the `addressbook` call back change, even without having changed anything in the addressbook. I didn't expect that. -.- – Moody Sep 08 '14 at 11:53

1 Answers1

1

I may be wrong,as so far I've only written for OSX where I did have a similar problem, which I eventually solved. (See link below)
I've a hunch your addressBook object is just a local copy. I found I had to get the addressBook using ABAddressBook *addressBook =[ABAddressBook sharedAddressBook]; and work with the records from that, finishing as you do with [addressBook save]; Your iOS usage may be different. (How can I get the values I set for custom properties in AddressBook to persist?)

Community
  • 1
  • 1
AlexT
  • 596
  • 7
  • 15
  • The iOS address book interface is quite different than OS X interface. The OP's use appears correct for iOS (other than the leaks). – Rob Sep 07 '14 at 21:11