Created
May 4, 2015 18:29
-
-
Save philipheinser/5dab6d2c58f051f691fd to your computer and use it in GitHub Desktop.
NSManagedDocument App Delegate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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