Database Reference
In-Depth Information
} else {
println ( "Could not fetch \(error) , \(error!. userInfo ) " )
}
}
Step by step, this is what the code does:
1. As mentioned in the previous section, before you can do anything with Core
Data, you need a managed object context. Fetching is no different! You pull up
the application delegate and grab a reference to its managed object context.
2. As the name suggests, NSFetchRequest is the class responsible for fetching from
Core Data. Fetch requests are both powerful and flexible. You can use requests to
fetch a set of objects that meet particular criteria (e.g., “give me all employees
that live in Wisconsin and have been with the company at least three years”),
individual values (e.g., “give me the longest name in the database”) and more.
Fetch requests have several qualifiers that refine the set of results they return.
You'll learn more about these qualifiers in Chapter 4, “Intermediate Fetching”; for
now, you should know that NSEntityDescription is one of these qualifiers (one
that is required!).
Setting a fetch request's entity property, or alternatively initializing it with
init(entityName:) , fetches all objects of a particular entity. This is what you do
here to fetch all Person entities.
3. You hand the fetch request over to the managed object context to do the heavy
lifting. executeFetchRequest(_:error:) returns an optional array of managed
objects that meets the criteria specified by the fetch request.
Note: If there are no objects that match the fetch request's criteria, the
method returns an optional value containing an empty array.
If an error occurred during the fetch, the method returns an optional value
that contains nil. If this happens, you can inspect the NSError and respond
appropriately.
Build and run the application once again. Immediately, you should see the list of
names you added earlier:
Search WWH ::




Custom Search