samedi 11 juillet 2015

How to replace a core data store on the fly?

When a given iOS app is first run or upgraded, a bundled .sqlite file is copied/overwritten to the core data DB file location in applicationDidFinishLaunching. It is then loaded with [MagicalRecord setupCoreDataStackWithStoreAtURL:] (using Magical Record).

My question is this: If a new .sqlite file is instead downloaded, how can I dump the existing store and repopulate the core data store while the app is running?

Preloading is done along these typical lines:

if(!appVerionKey || appVerionKey != currAppVesion) {
    // Copy to or overwrite the storeURL with the bundled sqlite file
    [[NSFileManager defaultManager] copyItemAtURL:preloadURL 
                                            toURL:storeURL 
                                            error:&err];

    // Load the store
    [MagicalRecord setupCoreDataStackWithStoreAtURL:storeURL]
}

(Note: Replacing individual entities is not an option. The whole core needs to be dumped.)

Aucun commentaire:

Enregistrer un commentaire