Database Reference
In-Depth Information
That's all you need to do. If you wanted to refine your fetch request with an
additional predicate, you could also add conditions from the fetch request editor.
Let's take your newly created fetch request out for a spin. Switch to
ViewController.swift and import the Core Data framework:
import CoreData
Then, add the following two properties to the top of the file:
//Add below var coreDataStack: CoreDataStack!
var fetchRequest: NSFetchRequest !
var venues: [ Venue ]!
The first property will hold your fetch request. The second property is the array of
Venue objects that you'll use to populate the table view.
Next, make the following additions to the currently empty viewDidLoad :
fetchRequest =
coreDataStack . model . fetchRequestTemplateForName ( "FetchRequest" )
fetchAndReload ()
The first line connects the fetchRequest property you set up moments ago to the
one you created using Xcode's data model editor. There are two things to
remember here:
1. Unlike other ways of getting a fetch request, this one involves the managed
object model. This is why you must go through CoreDataStack 's model property to
retrieve your fetch request.
2. NSManagedObjectModel 's fetchRequestTemplateForName() takes a string identifier.
This identifier must exactly match whatever name you chose for your fetch
request in the model editor. Otherwise, your app will throw an exception and
crash. Whoops!
The second line calls a method you haven't defined yet, so Xcode will complain
about it. Declare this method at the bottom of the file to appease Xcode:
 
Search WWH ::




Custom Search