fabian fabian - 14 days ago 5
Objective-C Question

CoreData: How to fetch a specific object using a predicate

The situation:

I fetch a complete table from my sqllite core data database and show it in a TableView like this:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyTable"
inManagedObjectContext:managedObjectContext];


The Challenge:

How do I get the EntryID and fetch the specific entry from the database (e.g. if i click on an entry)? I think this goes in the right direction?

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(id = %@)", myEntryID];

Answer

If you have an entry object called entry, it would be a predicate like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(SELF = %@)", entry];

Which is roughly equivalent to

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(objectID = %@)", entry.objectID];

For a NSManagedObjectID, you get something like:

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(objectID = %@)", myEntryID];