Database Reference
In-Depth Information
didFinishLaunchingWithOptions
launchOptions: [
NSObject
:
AnyObject
]?) ->
Bool
{
let
navigationController =
self
.
window
!.
rootViewController
as
UINavigationController
let
viewController =
navigationController.
topViewController
as
ViewController
viewController.
managedContext
=
coreDataStack
.
context
return
true
}
This code propagates the managed context from your
CoreDataStack
object
(initializing the whole stack in the process) to
ViewController
.
Finally, add the following two
UIApplicationDelegate
methods to the
AppDelegate
class:
func
applicationDidEnterBackground(application:
UIApplication
) {
coreDataStack
.
saveContext
()
}
func
applicationWillTerminate(application:
UIApplication
) {
coreDataStack
.
saveContext
()
}
These methods ensure that Core Data saves any pending changes before the app is
either sent to the background or terminated for whatever reason.
Modeling your data
Now that your shiny new Core Data stack is securely fastened to your application
delegate, it's time to create your data model.
Head over to your Project Navigator and… Wait a second. There's no data model
file! That's right. Since I generated this sample application without enabling the
option to use Core Data, there's no
.xcdatamodel
file.
No worries. Go to
File\New\File…
, select the
iOS\Core Data\Data Model
template and click
Next
. Name the file
Dog Walk.xcdatamodeld
and click
Create
to save the file: