Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 691
fabian Avatar asked Feb 21 '10 14:02

fabian


People also ask

What is predicate in Core Data?

For example, if you already completed project 33 you'll have seen how predicates let us find iCloud objects by reference. Put simply, a predicate is a filter: you specify the criteria you want to match, and Core Data will ensure that only matching objects get returned.

What is fetched property in Core Data?

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

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];
like image 52
MrMage Avatar answered Sep 27 '22 20:09

MrMage


This is proper predicate:

[NSPredicate predicateWithFormat:@"SELF = %@", objectID];

Where objectID is NSManagedObjectID instance.

like image 30
Maciek Czarnik Avatar answered Sep 27 '22 22:09

Maciek Czarnik