MwcsMac
MwcsMac

Reputation: 7168

Swift Core Data preload persistentStoreCoordinator:

What needs to be modified to preload my sqlite file? I added the file to the project so that makes me think I have to make a change in this code.

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
    // Create the coordinator and store
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("junkapp.sqlite")
    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
        coordinator = nil
        // Report any error we got.
        let dict = NSMutableDictionary()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        //error = NSError.errorWithDomain("YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

    return coordinator
}()

Upvotes: 0

Views: 2426

Answers (2)

MwcsMac
MwcsMac

Reputation: 7168

This is the final code that worked for me. Note the section //Copying and keep in mind that you will have to delete the app off device or simulator first before running this.

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
    // Create the coordinator and store
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("junkapp.sqlite")
    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    // Copying
    let path = NSBundle.mainBundle().pathForResource("junkapp", ofType:"sqlite")!
    NSFileManager.defaultManager().copyItemAtPath(path, toPath: url.path!, error:nil)
    //end copy
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: NSURL.fileURLWithPath(url.path!), options: nil, error: &error) == nil {
        coordinator = nil
        // Report any error we got.
        let dict = NSMutableDictionary()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        //error = NSError.errorWithDomain("YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

    return coordinator
}()

Upvotes: 1

Mundi
Mundi

Reputation: 80265

Just change the file url to point to your SQLite file.

You need to

  1. copy the SQLite file from the bundle to the documents directory.
  2. reference this file url in addPersistentStore....

e.g.

// Copying
let path = NSBundle.mainBundle().pathForResource("sqlitefile", ofType:"sqlite")!
let destinationPath = 
  self.applicationDocumentsDirectory.URLByAppendingPathComponent("junkapp.sqlite")!.path
NSFileManager.defaultManager().copyItemAtPath(
  path, toPath: destinationPath, error:nil)

// Using
coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, 
  configuration: nil, URL: NSURL.fileURLWithPath(destinationPath), 
  options: nil, error: &error)

Upvotes: 1

Related Questions