How to Get Core Data Entity by It's Objectid

How can I get core data entity by it's objectID?

You can't query arbitrary properties of the NSManagedObject with a predicate for a NSFetchRequest. This will only work for attributes that are defined in your entity.

NSManagedObjectContext has two ways to retrieve an object with an NSManagedObjectID. The first one raises an exception if the object does not exist in the context:

managedObjectContext.objectWithID(objectID) 

The second will fail by returning nil:

var error: NSError?
if let object = managedObjectContext.existingObjectWithID(objectID, error: &error) {
// do something with it
}
else {
println("Can't find object \(error)")
}

If you have a URI instead of a NSManagedObjectID you have to turn it into a NSManagedObjectID first. The persistentStoreCoordinator is used for this:

let objectID = managedObjectContext.persistentStoreCoordinator!.managedObjectIDForURIRepresentation(uri)

How to get Core Data object from specific Object ID?

You want:

-(NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID
error:(NSError **)error

Fetches the object from the store that has that ID, or nil if it doesn't exist.

(Be aware: there are two methods on NSManagedObjectContext with similar-seeming names that tripped me up. To help keep them straight, here's what the other two do:

-(NSManagedObject *)objectWithID:(NSManagedObjectID *)objectID

...will create a fault object with the provided objectID, whether or not such an object actually exists in the store. If it doesn't exist, anything that fires the fault will fail unless you insert the object first with NSManagedObjectContext's insertObject:. The only use I've found for this is copying objects from store to store while preserving ObjectIDs.

-(NSManagedObject *)objectRegisteredForID:(NSManagedObjectID *)objectID

...will return the object that has that ID, if it has been fetched from the store by this managedObjectContext. If anyone knows what this method is useful for, please comment.)

[eta.: Another important difference between the first method and the other two is that existingObjectWithID:error: never returns a fault; it always fetches the whole object for you. If you're trying to avoid that (e.g. working with an expensive-to-fetch object with a big blob property), you have to be clever with objectWithID: or objectRegisteredForID:, which don't fire faults; or use a properly configured fetch request.]

Core data, how to get NSManagedObject's ObjectId when NSFetchRequest returns NSDictionaryResultType?

Yes you can, using the very nifty but badly-documented NSExpressionDescription class. You need to add a properly-configured NSExpressionDescription object to the array of NSPropertyDescription objects you set via setPropertiesToFetch: for your NSFetchRequest.

For example:

NSExpressionDescription* objectIdDesc = [[NSExpressionDescription new] autorelease];
objectIdDesc.name = @"objectID";
objectIdDesc.expression = [NSExpression expressionForEvaluatedObject];
objectIdDesc.expressionResultType = NSObjectIDAttributeType;

myFetchRequest.propertiesToFetch = [NSArray arrayWithObjects:objectIdDesc, anotherPropertyDesc, yetAnotherPropertyDesc, nil];
NSArray* fetchResults = [myContext executeFetchRequest:myFetchRequest error:&fetchError];

You should then have a @"objectID" key in the the dictionaries you get back from your fetch request.

Core Data - fetch object with keypath and Object ID

You should get the object with existingObject from the Object ID and use that in the predicate.

if let myPerson = context.existingObject(with: myPersonObjectID) {
request.predicate = NSPredicate(format: "person = %@", myPerson)
}

Core data object id format

The p identifies the objectID as persistent, associated to its MOC. It is part of the whole URI.

Temporary URIs look different, e.g.:

x-coredata:///Facility/tF1697911-CD8A-4D63-B40F-AB0CA020C873

notice the "t" in front of the objectID.

That's just how CoreData's url scheme works.

You have 2 persistent unique IDs to differentiate the object references.

Can I access CoreData objects by index?

Credit to @JohnElemans but he posted a comment rather than an answer so I'm posting this for others benefit.

Loading the objects from the Managed Object Context into an array (or dictionary) passes them by reference.

In the example above, assuming there is an object in presidents[5], it can be accessed in the conventional way.

presidents[5] = "Hilary"

objectID not found error while fetching NSManagedObject context in Core Data

To access an NSManagedObject based on the objectID, you only need to write

appDelegate.coreDataStack.managedObjectContext.object(with: selectedPlayerID)

Check it here Apple Doc object(with:)



Related Topics



Leave a reply



Submit