Saturday, March 29, 2014

Managed Object ID


  • Get the string URL representation from a ManagedObjectID
1
2
  NSURL *urlID = [objectID URIRepresentation];
  NSString *strUrlID = [urlID absoluteString];

  • Get the ManagedObjectID from a URL string
1
2
  NSURL *urlID = [[NSURL alloc] initWithString:strUrlID];
  NSManagedObjectID *objectID = [[managedObjectContext persistentStoreCoordinator] managedObjectIDForURIRepresentation:urlID];

  • Get the ManagedObject by a ManagedObjectID
1
2
  NSError *error = nil;
  NSManagedObject *managedObject = [moc existingObjectWithID:objectID error:&error];

  • Check if a ManagedObject is new
1
  BOOL isNew = managedObject.objectID.isTemporaryID;

  • Get the permanent object IDs for the objects in a child context.
1
2
3
4
5
6
7
8
  NSError *error = nil;
  [childManagedObjectContext obtainPermanentIDsForObjects:childManagedObjectContext.insertedObjects.allObjects error:&error]);

  // . . . Check for an error . . .

  [childManagedObjectContext save:&error];

  // . . . Check for an error . . .


Refs
http://stackoverflow.com/questions/3519334/how-to-uniquely-identify-nsmangedobject-with-string
http://stackoverflow.com/questions/10811280/how-to-get-the-objectid-from-url
http://stackoverflow.com/questions/3013288/how-to-use-managedobjectid-the-right-way
http://iphonedevelopment.blogspot.com.au/2009/07/core-data-determining-if-managed-object.html
http://stackoverflow.com/questions/11990279/core-data-do-child-contexts-ever-get-permanent-objectids-for-newly-inserted-obj/11996957#11996957

No comments:

Post a Comment