2014-01-30 106 views
0

我想存储ABRecordRef对象,即使用Coredata格式为<CPRecord: 0xa2a3500 ABPerson>如何在CoreData中存储ABRecordRef对象

虽然我试图像存储

NSEntityDescription *entityDescriptionStrings = [NSEntityDescription entityForName:@"ContactsStrings" inManagedObjectContext:context]; 

ContactsStrings *contactsManagedObjectStrings = [[ContactsStrings alloc] initWithEntity:entityDescriptionStrings insertIntoManagedObjectContext:context]; 

     ABRecordRef recordRef = CFArrayGetValueAtIndex(contactInfoArray, i); 
[contactsManagedObjectStrings setValue:(__bridge id)(recordRef) forKey:@"record"]; 

我得到一个崩溃说

记录我都当作位整数数据类型。

终止应用程序由于未捕获的异常NSInvalidArgumentException

原因:“的属性不接受的类型的值:属性= “记录”;期望的类型= NSNumber;给定类型= __NSCFType;值= 。

+0

尝试使用'[NSNumber的numberWithInt:(INT)recordRef]'代替'(__bridge ID)(recordRef)' – Akhilrajtr

+0

@Akhilrajtr:我将能够存储,但是,我如何从NSNumber取回ABrecordRef对象 – iOSDev

回答

0

最好不要保存ABRecordRef,但是你应该在你的coredata中保存ABRecordRefID。这里是link这可以给你更多的细节。我总是喜欢这样做。

0

问题是您正在数据库中存储对象引用。参考指向的内存仅在时间点有效。如果存储对象引用,然后从数据库重新加载引用(特别是在重新启动应用程序后),它将指向无效内存。

而是将来自对象的实际数据存储到数据库中,而不是从ABRecordGetRecordID()返回的对象引用或记录标识。

1

尝试此,

ABRecordRef recordRef = CFArrayGetValueAtIndex(contactInfoArray, i); 
ABRecordId recId = ABRecordGetRecordID(recordRef); 
NSNumber *recordId = [NSNumber numberWithInt:(int)recId]; 
[contactsManagedObjectStrings setValue:recordId forKey:@"record"]; 

以检索

//recordId is the value of record key from managedobject 
ABRecordId recId = (ABRecordId)[recordId intValue]; 
ABRecordRef recordRef = ABAddressBookGetPersonWithRecordID(ddressBook, recId);