Alex Rivera | Logout

NSManagedObjectID into NSData

Asked 2009-02-05T15:44:51.307
14

I found this wonderful NSManagedObjectID. This would be very good for referencing an Entity/NSManagedObject/NSEntityDescription, right?
Let's get an ID from an entity:

NSEntityDescription *entity = [self newEntity];     
NSManagedObjectID *objID = [entity objectID];

So... any idea how to get this objID into a string? Or better: NSData. Actually something to be able to save it to the NSUserDefaults. ;-)

Btw: NSFetchRequest doesn't want to work in my case. I use an modified version of this example: answer of an old question.

Edit
Report

2 Answers

5

From the NSManagedObjectID documentation:

Object IDs can be transformed into a URI representation which can be archived and recreated later to refer back to a given object (using managedObjectIDForURIRepresentation: (NSPersistentStoreCoordinator) and objectWithID: (NSManagedObjectContext). For example, the last selected group in an application could be stored in the user defaults through the group object’s ID. You can also use object ID URI representations to store “weak” relationships across persistent stores (where no hard join is possible).

Just turn it into a URL then turn that into a string or a data.

answered 2009-02-05T16:37:06.633
3

Did you look at URIRepresentation? It's easy to convert an NSURL to an NSString, and that to an NSData.

answered 2009-02-05T16:28:56.710

Your Answer