Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Fetch Request Variable in Xcode 4

Tags:

ios

core-data

How do I add a variable to a fetch request in core data in Xcode 4? Can't find it.

like image 519
codeetcetera Avatar asked Jun 07 '11 19:06

codeetcetera


People also ask

What is Core Data fetch request?

Fetch requests allow us to load Core Data results that match specific criteria we specify, and SwiftUI can bind those results directly to user interface elements. If you followed my Core Data and SwiftUI set up instructions, you've already injected your managed object context into the SwiftUI environment.

How do I get data from Core Data?

Fetching Data From CoreData We have created a function fetch() whose return type is array of College(Entity). For fetching the data we just write context. fetch and pass fetchRequest that will generate an exception so we handle it by writing try catch, so we fetched our all the data from CoreData.

What is Nsfetchrequest in Swift?

A description of search criteria used to retrieve data from a persistent store.

How do I get NSManagedObject?

Still inside the Core Data editor, go to the Editor menu and choose Create NSManagedObject Subclass. Make sure your data model is selected then click Next. Make sure the Commit entity is checked then click Next again.


1 Answers

Select Expression from the drop down and enter the expression using a $ before your substitution variable (NAME in the example below). Even if the substitution variable's value is a string, make sure you don't put the variable between quotes, or the substitution will not work.

Fetch predicate with substitution variable

In your code you refer to the fetch predicate like this (XCode 4.4 and above):

NSManagedObjectModel* model = [[context persistentStoreCoordinator] managedObjectModel];
NSFetchRequest* request = [model fetchRequestFromTemplateWithName:templateName
                                            substitutionVariables:@{@"NAME" : name}];
NSError* error = nil;
NSArray* results = [context executeFetchRequest:request error:&error];
like image 162
ovidiu Avatar answered Oct 15 '22 19:10

ovidiu