Reputation: 111
I am trying to store my unique records using CoreData ObjectId.Sometimes i am succeded to get ObjectID in this format
<x-coredata://FC004E6F-F3D6-420E-871E-3E281571FB8B/Register/p4>
through which i am able to retreive the id i.e 4.But sometimes it return in this format of ObjectId
<x-coredata:///Playlist/tC481615F-4ABA-4CDA-B703-D1E6303B455D3>
i.e NSTemporaryObjectID_Default which is so irritating.I clean my project from my IOS Simulator,Clean Xcode but it is not working perfectly.
This is my code to get object ID:
-(IBAction)TestButton:(id)sender
{
appObject=(AppDelegate*)[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context =[appObject managedObjectContext];
NSManagedObject *playlistContact;
NSManagedObjectID *moID = [playlistContact objectID];
NSString *aString = [[moID URIRepresentation] absoluteString];
NSArray *theComponents = [aString componentsSeparatedByString:@"/p"];
NSInteger theZpk = [[theComponents lastObject] intValue];
playlistId=[NSString stringWithFormat:@"%ld",(long)theZpk];
}
Please help me.
Upvotes: 0
Views: 2189
Reputation: 16664
To get objectID
use:
-(NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID
error:(NSError **)error
To get permanent objectID
use obtainPermanentIDsForObjects
, for example:
NSManagedObject *contact = [NSEntityDescription insertNewObjectForEntityForName:self.entityName inManagedObjectContext:context];
NSError *error = nil;
if (![context obtainPermanentIDsForObjects:
@[contact] error:&error]) {
NSLog(@"Couldn't obtain a permanent ID for object %@",
error);
}
Upvotes: 1
Reputation: 46718
You should not be using ObjectID
for that. If you are looking to use a primary key you really should be creating your own primary key and fetching off of that instead.
The ObjectID
can and will change on you in several different situations causing numerous problems. It is intended to be used within the application during one life-cycle of the app. Using it across life cycles is not recommended.
Upvotes: 3