Leonardo
Leonardo

Reputation: 9857

Save NSManagedObjectID in CoreData

I have an xcdatamodel with a set of entities built in a static library.

I am including this static library in a project. I would like to create another model in main project, with entity named Task. I would like to have an attribute in the entity where I could store the NSManagedObjectID of an entity created in static library. With NSManagedObjectID I could easily fetch the main store and get the entity. In the end there could be many Task entities refer to an objectID.

Is it possible ? I also understand that this is sounds like a relational model, which Core Data isn't, so is there a better solution for dealing with the subject?

thanks

Upvotes: 2

Views: 2619

Answers (2)

PeyloW
PeyloW

Reputation: 36752

The NSManagedObjectID is in itself not coding compliant. But you can get the URL representation of an object ID and store that. Like this:

myObject.externalTaskURL = [[task objectID] URIRepresentation];

Then in order to get the object ID back to retrieve the task object it refers to later (psc is your NSPersistentStoreCoordinator where the Task entities live):

NSManagedObjectID* taskID = 
        [psc managedObjectIDForURIRepresentation:myObject.externalTaskURL];

Make sure to never do this to a temporary managed object ID.

Upvotes: 12

TechZen
TechZen

Reputation: 64428

You can store a managed object ID as a NSURL in a transformable attribute.

Upvotes: -1

Related Questions