dimanche 3 janvier 2016

How to replace pre-populated core data in iOS app with new version from the app store?

I included preloaded core data in my app as 3 bundle files (.sqlite, .sqlite-shm, .sqlite-wal), and used them as the main core data when the user downloads the app.

This is what i have inside AppDelegate.swift

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("coreDB.sqlite")




    /******************** If core data is empty, import from the bundle *******************/
    if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
        let sourceSqliteURLs = [NSBundle.mainBundle().URLForResource("coreDB", withExtension: "sqlite")!, NSBundle.mainBundle().URLForResource("coreDB", withExtension: "sqlite-wal")!, NSBundle.mainBundle().URLForResource("coreDB", withExtension: "sqlite-shm")!]

        let destSqliteURLs = [self.applicationDocumentsDirectory.URLByAppendingPathComponent("coreDB.sqlite"),
            self.applicationDocumentsDirectory.URLByAppendingPathComponent("coreDB.sqlite-wal"),
            self.applicationDocumentsDirectory.URLByAppendingPathComponent("coreDB.sqlite-shm")]

        var error:NSError? = nil
        for var index = 0; index < sourceSqliteURLs.count; index++ {
            do {
                try NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURLs[index], toURL: destSqliteURLs[index])
            } catch var error1 as NSError {
                error = error1
            } catch {
                fatalError()
            }
        }
    }


var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    do {
        try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true])
    } catch var error1 as NSError {
        error = error1
        coordinator = nil
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        error = NSError(domain: "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()
    } catch {
        fatalError()
    }

    return coordinator
}()

Now I had to change some data from core data, so this is what I did:

1) Change some data from core data using xcode so the data gets changed when the app is ran in iOS simulator

2) Find the .sqlite, .sqlite-shm, sqlite-wal files, which belong to the iOS simulator

3) Drag these 3 files into xcode and include them in the app's bundle

4) How can i make the newly updated app replace its core data with these 3 files when the user updates the app? Is something called Lightmigration the solution for such problem?

Aucun commentaire:

Enregistrer un commentaire