Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving the NSManagedObjectID from one entity as a property on a different Entity?

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

like image 467
RickiG Avatar asked Dec 04 '25 10:12

RickiG


1 Answers

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.

like image 97
Alex Reynolds Avatar answered Dec 07 '25 00:12

Alex Reynolds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!