ABAddressBookRegisterExternalChangeCallback works, but data is stale

狂风中的少年 提交于 2019-12-21 10:44:09

问题


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?


回答1:


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);    
    }


来源:https://stackoverflow.com/questions/7138718/abaddressbookregisterexternalchangecallback-works-but-data-is-stale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!