Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ManagedObjectID the right way?

What I'm trying is this:

1) Create a new manged object

2) Get it's temporary id with [myMO objectID];

3) Convert that ID to an NSURL, so I can save it for future reference:

NSManagedObjectID *moID = [myMO objectID];
NSURL *url = [moID URIRepresentation];

4) Save the managed object context

5) Some time later, fetch that object using the NSURL as ID

NSManagedObjectID *moID = [[context persistentStoreCoordinator] managedObjectIDForURIRepresentation:url];

And guess what: It does not work. I get an empty-stupid object back from

NSManagedObject *myOldMo = [context existingObjectWithID: moID error:&error];

But...as I said...the ID is temporary when creating an managed object. So it does make sense why this doesn't work at all. I must first save the context, and then I get a persistet ID. The real one. Right?

So is that the way to go?

1) Create the managed object

2) Save the context

3) Get the ID as NSURL

4) any time later, for example on your next birthday, access the managed object with the NSURL ;-)

I try to dream of NSManagedObjectID like a DB id which I can write on some yellow postIt sheet and glue on the middle of my monitor, so I refer back to it after lunch. You know... at least like in the old days where we used databases over telnet and executed SQL commands manually to query order information and stuff like that. The ID was the most important and significant thing, all the time.

But Core Data has this somewhat strange NSManagedObjectID thing.

What are your secret strategies? Do you actually recognize many use cases where you would need that NSManagedObjectID? Or is that something I could easily forget with no pain afterwards?

like image 489
dontWatchMyProfile Avatar asked Jun 10 '10 09:06

dontWatchMyProfile


People also ask

How do I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What is the purpose of managed object context NSManagedObjectContext?

A managed object context is an instance of NSManagedObjectContext . Its primary responsibility is to manage a collection of managed objects. These managed objects represent an internally consistent view of one or more persistent stores.

How do you start your NSFetchRequest?

You initialize an instance of NSFetchRequest as generic type: NSFetchRequest<Venue> . At a minimum, you must specify a NSEntityDescription for the fetch request. In this case, the entity is Venue . You initialize an instance of NSEntityDescription and use it to set the fetch request's entity property.

How do I set up NSManagedObject?

From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both a class and a properties file into your project.


1 Answers

I'm not sure that it's such a big secret. The documentation describes the way to get permanent IDs for managed objects from the NSManagedObjectContext:

- (BOOL)obtainPermanentIDsForObjects:(NSArray *)objects error:(NSError **)error

http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/occ/instm/NSManagedObjectContext/obtainPermanentIDsForObjects:error:

like image 100
ohhorob Avatar answered Sep 20 '22 22:09

ohhorob