views:

244

answers:

1

Hi

I have a Core Data Entity that needs to hold onto the NSManagedObjectID of some other Entity. To do so I was considering converting the ObjectID to a string that is an approved type of an NSManagedObject attribute.

I can read from the documentation that I can get a URI representation of the ID by:

NSURL *uriID = [[myEntity objectID] URIRepresentation];

I can then convert this URL to an NSString by:

NSString *stringID = [uriID absoluteString];

This I can persist to my NSManagedObject's NSString attribute.

Now what happens when I need to go the other way?

I would like to be able to do something like this:

if([myManagedObject objectID] == value) 

where value is the NSManagedObjectID that I converted to an NSString earlier.

To shed a little more light on the why: I need to be able to have an Entity object hold and persist the ObjectID of another Entity object, so that I later on can go: this Objects last "interaction" was with this Entity.

Hope someone can help me get this working:) Thank you

+3  A: 

Why not just establish a to-one relationship property in Object called interaction, which points to an instance of an Entity — and vice verse, a to-many relationship from Entity to Object called interactions? This solves the problem pretty neatly, without all the conversion methods.

But you might also look at the -managedObjectIDForURIRepresentation: and +URLWithString: methods to go the other direction.

Alex Reynolds
Hi Alex, thank you. I was looking in the NSManagedObject and NSManagedObject documentation, but now it makes sense it resides in the NSPersistentStoreCoordinator docs, since it will be the one who pulls the object from the store:)It is a bit more complicated than described, I also have more than one model and I only need to wrap this "check" for one "freak accident" use case.Thank you again, I'll get right on implementing it:)
RickiG