我的应用程序注册了一次回调:
notificationAddressBook = ABAddressBookCreate(); ABAddressBookRegisterExternalChangeCallback(notificationAddressBook,MyAddressBookExternalChangeCallback,self);
然后在我的回调中:
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); }
添加新联系人时,事件被触发正常,并且数据在第一次添加和第二次和第三次添加时是最新的.问题在于编辑现有联系人的详细信息.
第一次触发事件时数据对上次更新是正确的(我更改了iPhone联系人中一个联系人的电话号码),然后切换到应用程序并获取最新更新.然后我切换回地址簿,进行另一次更改,切换到我的应用程序并获得另一个事件.这次数据陈旧,最新的变化没有反映出来.
我尝试释放ABAddressBookRef实例并再次调用ABAddressBookCreate(),但它也没有帮助.
有任何想法吗?
解决方法
尝试重新创建ABAddressBookRef.
void MyAddressBookExternalChangeCallback (ABAddressBookRef notifyAddressBook,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); }