Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ABAddressBookRegisterExternalChangeCallback works, but data is stale

My app registers the callback once:

notificationAddressBook = ABAddressBookCreate();

ABAddressBookRegisterExternalChangeCallback(notificationAddressBook, MyAddressBookExternalChangeCallback, self);

Then in my callback:

void MyAddressBookExternalChangeCallback (ABAddressBookRef notifyAddressBook,CFDictionaryRef info,void *context)
{
     NSLog(@"in MyAddressBook External Change Callback");

     ABAddressBookRevert(notifyAddressBook);         

     CFArrayRef peopleRefs = ABAddressBookCopyArrayOfAllPeopleInSource(notifyAddressBook, kABSourceTypeLocal);

     CFIndex count = CFArrayGetCount(peopleRefs);
     NSMutableArray* people = [NSMutableArray arrayWithCapacity:count];
     for (CFIndex i=0; i < count; i++) {
        ABRecordRef ref = CFArrayGetValueAtIndex(peopleRefs, i);
        ABRecordID id_ = ABRecordGetRecordID(ref);
        TiContactsPerson* person = [[[TiContactsPerson alloc] _initWithPageContext:[context executionContext] recordId:id_ module:context] autorelease];
        NSLog(@"name: %@", [person valueForKey:@"firstName"]);
        NSLog(@"phone: %@", [person valueForKey:@"phone"]);
        NSLog(@"modified: %@", [person valueForKey:@"modified"]);
        [people addObject:person];
     } 

     CFRelease(peopleRefs);
}

When adding a new contact, the event is triggered fine, and the data is up-to-date in the first addition and the second and third. The problem is with editing an existing contact's details.

The first time the event is triggered the data is correct to the last update (I changed the phone number of one contact in the iPhone contacts), then I switch to the app and get the latest update. Then I switch back to the address book, make another change, switch to my app and get another event. This time the data is stale, the latest changes are not reflected.

I tried releasing the ABAddressBookRef instance and call ABAddressBookCreate() again but it did not help either.

Any ideas?

like image 355
Moshe Marciano Avatar asked Aug 21 '11 14:08

Moshe Marciano


1 Answers

Try to re-create ABAddressBookRef.

    void MyAddressBookExternalChangeCallback (ABAddressBookRef notifyAddressBook,CFDictionaryRef info,void *context)
    {
         NSLog(@"in MyAddressBook External Change Callback");

         //ABAddressBookRevert(notifyAddressBook);
         notifyAddressBook = ABAddressBookCreate();

         CFArrayRef peopleRefs = ABAddressBookCopyArrayOfAllPeopleInSource(notifyAddressBook, kABSourceTypeLocal);

         CFIndex count = CFArrayGetCount(peopleRefs);
         NSMutableArray* people = [NSMutableArray arrayWithCapacity:count];
         for (CFIndex i=0; i < count; i++) {
            ABRecordRef ref = CFArrayGetValueAtIndex(peopleRefs, i);
            ABRecordID id_ = ABRecordGetRecordID(ref);
            TiContactsPerson* person = [[[TiContactsPerson alloc] _initWithPageContext:[context executionContext] recordId:id_ module:context] autorelease];
            NSLog(@"name: %@", [person valueForKey:@"firstName"]);
            NSLog(@"phone: %@", [person valueForKey:@"phone"]);
            NSLog(@"modified: %@", [person valueForKey:@"modified"]);
            [people addObject:person];
         } 

         CFRelease(peopleRefs);    
    }
like image 82
ChangUZ Avatar answered Oct 19 '22 22:10

ChangUZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!