Skip to content

Instantly share code, notes, and snippets.

@philipheinser
Created May 4, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipheinser/5dab6d2c58f051f691fd to your computer and use it in GitHub Desktop.
Save philipheinser/5dab6d2c58f051f691fd to your computer and use it in GitHub Desktop.
NSManagedDocument App Delegate
let fileManager = NSFileManager.defaultManager()
let directoryArray = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let directoryUrl = directoryArray[0] as! NSURL
let documentName = "data-store"
let url:NSURL = directoryUrl.URLByAppendingPathComponent(documentName)
var document = UIManagedDocument(fileURL: url)
let fileExists = fileManager.fileExistsAtPath(url.path!)
if fileExists {
document.openWithCompletionHandler { (success) -> Void in
self.managedContext = document.managedObjectContext
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ItemsTableViewController") as! UIViewController
self.window?.rootViewController = vc
}
} else {
document.saveToURL(url, forSaveOperation: UIDocumentSaveOperation.ForCreating, completionHandler: { (success) -> Void in
self.managedContext = document.managedObjectContext
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ItemsTableViewController") as! UIViewController
self.window?.rootViewController = vc
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment