0
votes

I want to store a ABRecordRef Object i.e in the format of <CPRecord: 0xa2a3500 ABPerson> using Coredata.

While I have tried to store like

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"];

I am getting a crash saying

record I have taken as Integer32 Datatype.

Terminating app due to uncaught exception NSInvalidArgumentException,

reason: 'Unacceptable type of value for attribute: property = "record"; desired type = NSNumber; given type = __NSCFType; value = .

3
Try using [NSNumber numberWithInt:(int)recordRef] instead of (__bridge id)(recordRef)Akhilrajtr
@Akhilrajtr : I would be able to store that but, How can i get back the ABrecordRef object from NSNumberiOSDev

3 Answers

1
votes

Try this,

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

to retrive

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

It will better you don't save ABRecordRef, but you should save ABRecordRefID in your coredata. Here is the link which can give you more detail in this. I always prefer to do in that way.

0
votes

The issue is that you are storing an object reference within a database. A reference points to memory that is valid at only that point in time. If you store the object reference and then later reload the reference from the database (especially after an app restart), it will point to invalid memory.

Instead store the actual data from the object into the database, and not the object reference, or perhaps the record ID, as returned from ABRecordGetRecordID().