iOS Move CoreData location to App Group

Desbrina

macrumors newbie
Original poster
Nov 23, 2019
1
0
Hertfordshire,UK
I'm trying to move my core data save location to an app group to share it across to an Apple Watch app.
The app is already released so the original data needs to be kept.

So far I've tried
Code:
func migrateStore() {
        persistentContainer = NSPersistentContainer(name: "Diamond_Painting_Logbook")
        persistentContainer.loadPersistentStores { (description, error) in
            if let error = error {
                fatalError("Could not create CoreData store: \(error)")
            }

            var nURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.Desbrina.dpl.Diamond-Painting-Logbook.shared")
            nURL = nURL?.appendingPathComponent("Diamond_Painting_Logbook.sqlite")

            if let newURL = nURL {
                do {
                    let psc = self.persistentContainer.persistentStoreCoordinator
                    let store = psc.persistentStores[0]
                    NSLog("7 *****************")
                    try psc.migratePersistentStore(store, to: newURL, options: nil, withType: NSSQLiteStoreType)
                    NSLog("8 *****************")
                } catch {
                    NSLog("Unable to make new file \(error.localizedDescription)")
                }
            }
        }
    }
but its just producing an error
2019-11-23 22:35:18.512406+0000 Diamond Painting Logbook[6899:1865047] 7 *****************

2019-11-23 22:35:18.512969+0000 Diamond Painting Logbook[6899:1865047] [logging-persist] cannot open file at line 43353 of [378230ae7f]

2019-11-23 22:35:18.512997+0000 Diamond Painting Logbook[6899:1865047] [logging-persist] os_unix.c:43353: (0) open(/private/var/mobile/Containers/Shared/AppGroup/BF375BFE-4AF0-4F78-BDEE-C4A4A12493B3/Diamond_Painting_Logbook.sqlite) - Undefined error: 0

2019-11-23 22:35:18.513012+0000 Diamond Painting Logbook[6899:1865047] [logging] API call with unopened database connection pointer

2019-11-23 22:35:18.513025+0000 Diamond Painting Logbook[6899:1865047] [logging] misuse at line 162611 of [378230ae7f]

2019-11-23 22:35:18.513283+0000 Diamond Painting Logbook[6899:1865047] [error] error: -addPersistentStoreWithType:SQLite configuration:pF_DEFAULT_CONFIGURATION_NAME URL:file:///private/var/mobile/Containers/Shared/AppGroup/BF375BFE-4AF0-4F78-BDEE-C4A4A12493B3/Diamond_Painting_Logbook.sqlite/ options:{

NSPersistentStoreRemoveUbiquitousMetadataOption = 1;

} ... returned error NSCocoaErrorDomain(256) with userInfo dictionary {

NSFilePath = "/private/var/mobile/Containers/Shared/AppGroup/BF375BFE-4AF0-4F78-BDEE-C4A4A12493B3/Diamond_Painting_Logbook.sqlite";

NSSQLiteErrorDomain = 14;

}

CoreData: error: -addPersistentStoreWithType:SQLite configuration:pF_DEFAULT_CONFIGURATION_NAME URL:file:///private/var/mobile/Containers/Shared/AppGroup/BF375BFE-4AF0-4F78-BDEE-C4A4A12493B3/Diamond_Painting_Logbook.sqlite/ options:{

NSPersistentStoreRemoveUbiquitousMetadataOption = 1;

} ... returned error NSCocoaErrorDomain(256) with userInfo dictionary {

NSFilePath = "/private/var/mobile/Containers/Shared/AppGroup/BF375BFE-4AF0-4F78-BDEE-C4A4A12493B3/Diamond_Painting_Logbook.sqlite";

NSSQLiteErrorDomain = 14;

}

2019-11-23 22:35:18.516830+0000 Diamond Painting Logbook[6899:1865047] Unable to make new file: The file “Diamond_Painting_Logbook.sqlite” couldn’t be opened.


How do I move the store correctly?