Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a list objects from coredata and then I get objectId from one of those objects:

let fetchedId = poi.objectID.URIRepresentation()

Now I need to get entity for this specific objectID. And I tried something like:

let entityDescription = NSEntityDescription.entityForName("Person", inManagedObjectContext: managedObjectContext!);

        let request = NSFetchRequest();
        request.entity = entityDescription;

        let predicate = NSPredicate(format: "objectID = %i", fetchedId);

        request.predicate = predicate;

        var error: NSError?;

        var objects = managedObjectContext?.executeFetchRequest(request,
            error: &error)

But I get error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath objectID not found in entity <NSSQLEntity Person id=4>'
like image 209
1110 Avatar asked Mar 16 '15 11:03

1110


People also ask

What class would you use to filter fetched results from Core Data?

You can use . filter on the fetchedResults. Yes you can, but that's a Swift function. Using NSDelegate delegates filtering to Core Data which can be more efficient, especially on large data sets.

What is Nsmanagedobjectcontext in Core Data?

An object space to manipulate and track changes to managed objects.

What is a Core Data fetched property?

Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.


2 Answers

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)
like image 82
Matthias Bauch Avatar answered Oct 02 '22 16:10

Matthias Bauch


What you get is not the object ID, but the URI. The object ID is a part of the URI. You can ask the persistent store coordinator for the object ID with - managedObjectIDForURIRepresentation:. Having the object ID you can get the object from the context using for example -objectWithID:. But please look to the documentation, of this methods for some reasons.

like image 25
Amin Negm-Awad Avatar answered Oct 02 '22 17:10

Amin Negm-Awad