Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ABAddressBookRegisterExternalChangeCallback called several times

I have a strange problem where I register my iOS-app to listen to changes in the phones address book. The correct method is called when something changes in the address book but it gets called 2 - 6 times.

When the object gets created (singleton, so only one object), I register for notifications with this code:

ABAddressBookRegisterExternalChangeCallback(notificationAddressBook, addressBookChanged, (__bridge_retained  void *)self);

The method that is called looks like this:

void addressBookChanged(ABAddressBookRef ab, CFDictionaryRef info, void *context){
ABAddressBookRevert(ab);

    NSLog(@"ADDRESSBOOK CHANGED");
    [phoneBookCopy updateCopy];
}

Any ideas how to solve this?

like image 707
joakimb Avatar asked Apr 10 '12 21:04

joakimb


2 Answers

try this:

void addressBookChanged(ABAddressBookRef ab, CFDictionaryRef info, void *context){
ABAddressBookRevert(ab);

    NSLog(@"ADDRESSBOOK CHANGED");
    [phoneBookCopy updateCopy];
    CFRelease(ab);
}

It was helped for me.

like image 112
rusBogun Avatar answered Nov 20 '22 00:11

rusBogun


I had the same problem awhile ago, and I had to solve it by creating a NSTimer to handle the duplicate callbacks:

[self.changeTimer invalidate];
self.changeTimer = nil;
self.changeTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
                                                            target:self
                                                          selector:@selector(handleAdressBookExternalCallbackBackground)
                                                          userInfo:nil
                                                           repeats:NO];
like image 31
Z S Avatar answered Nov 20 '22 01:11

Z S