CoreData Swift 3 Xcode 8 NSPersistentContainer

Okay;

In the CoreDataBooks Sample Code it is shown “for the sake of illustration”

how to copyItemAtURL:_ toURL:storeURL and then addPersistentStoreWithType:_:_:storeURL

which I pasted below for your review along with its link.


Now, how do I do this in Swift 3 Xcode 8 with the use of the new NSPersistentContainer?

Do I do this in the storeDescription:? and if so how.


Could you give me a sample snipped in swift 3 showing me how to accomplish this from CoreDataBooks sample to Xcode 8 style?

Also, do we now keep the Core Data Stack in the app delegate or should we move it to its own class as before?

Thank you


https://developer.apple.com/library/ios/samplecode/CoreDataBooks/Listings/Classes_CoreDataBooksAppDelegate_m.html#//apple_ref/doc/uid/DTS40008405-Classes_CoreDataBooksAppDelegate_m-DontLinkElementID_8


NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataBooks.CDBStore"];

/*

Set up the store.

For the sake of illustration, provide a pre-populated default store.

*/

NSFileManager *fileManager = [NSFileManager defaultManager];

// If the expected store doesn't exist, copy the default store.

if (![fileManager fileExistsAtPath:[storeURL path]]) {

NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"CoreDataBooks" withExtension:@"CDBStore"];

if (defaultStoreURL) {

[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];

}

}

.......

NSError *error;

if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

.......

return _persistentStoreCoordinator;

}

Have you had any luck with this? I am looking for the same guidance.


Thanks


Greg

I resolved it with the following code:


    lazy var strengthPersistentContainer: NSPersistentContainer =
    {
        let container = NSPersistentContainer(name: "Favs")
     
        let sqlFileName: String = "Strength_v1_6"
        let sqlFileExtension: String = "sqlite"
         
        let url = self.applicationDocumentsDirectory.appendingPathComponent(sqlFileName + "." + sqlFileExtension)
         
        if (!FileManager.default.fileExists(atPath: url.path))
        {
            let defaultStorePath = Bundle.main.path(forResource: sqlFileName, ofType:sqlFileExtension)
            do
            {
                try FileManager.default.copyItem(atPath: defaultStorePath!, toPath:url.path)
            }
            catch _
            {
                 
            }
        }
        container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: url)]
        container.loadPersistentStores(completionHandler:
            {
                (storeDescription, error) in
                if let error = error as NSError?
                {
                    fatalError("Unresolved error \(error), \(error.userInfo)")
                }
            }
        )
        return container
    }()

This looks good, it may help with what I've been tring, I'll give it a try and get back with you, thanks.

Accepted Answer

This is what I came up with;

....

let container = NSPersistentContainer(name: “MyApp”)

var persistentStoreDescriptions: NSPersistentStoreDescription

let directory = NSPersistentContainer.defaultDirectoryURL()

let storeUrl = directory.appendingPathComponent("My_App.sqlite")

if FileManager.default.fileExists(atPath: (storeUrl.path)) {

let seededDataUrl = Bundle.main.url(forResource: "My_App", withExtension: "sqlite")

do {

try FileManager.default.copyItem(at: seededDataUrl!, to: storeUrl)

} catch let error as NSError {

print("Error: \(error.domain)")

}

}


let description = NSPersistentStoreDescription()

description.url = storeUrl


container.persistentStoreDescriptions = [description]

container.loadPersistentStores(completionHandler: { (storeDescription, error) in

if let error = error as NSError? {

print("Error: \(error.domain)")

}

})

return container

}()


// add a .removeItemAtURL(storeUrl) after the .fileExsts if need be

CoreData Swift 3 Xcode 8 NSPersistentContainer
 
 
Q