ios – How you can change present Core Information sqlite file with a brand new one?

0
77


That is how I setup my Core Information container and attempting to exchange empty one with a brand new one not empty:

init() {
        container = NSPersistentContainer(title: "MyApp")
        let choices = NSPersistentStoreDescription()
        choices.setOption(true as NSNumber, forKey: NSMigratePersistentStoresAutomaticallyOption)
        choices.setOption(true as NSNumber, forKey: NSInferMappingModelAutomaticallyOption)
        let containerUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: SharedGroupName)!
        let url = containerUrl.appendingPathComponent("MyApp.sqlite")
        container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: url), options]
        container.loadPersistentStores { _, error in
            if let error { fatalError(error.localizedDescription) }
        }
        if let url = container.persistentStoreDescriptions.first?.url {
            let coordinator = container.persistentStoreCoordinator
            coordinator.performAndWait {
                if let retailer = coordinator.persistentStores.first {
                    if let newDatabaseUrl = Bundle.primary.url(forResource: "MyApp", withExtension: ".sqlite") {
                        do {
                            attempt container.persistentStoreCoordinator.take away(retailer)
                            attempt FileManager.default.replaceItemAt(url, withItemAt: newDatabaseUrl)
                        } catch {
                            print("❌ error (error)")
                        }
                    }
                }
            }
        }
    }
}

That is an error printed on console:

❌ error Error Area=NSCocoaErrorDomain Code=512 “The file “MyApp.sqlite” couldn’t be saved within the folder “11C96DDE-7820-4053-AADE-4AAE1E7F3731”.” UserInfo={NSFileNewItemLocationKey=file:///personal/var/containers/Bundle/Utility/02E8B158-CE4A-4D3E-97A8-A04E19ED99A2/MyApppercent20Watchpercent20App.app/MyApp.sqlite, NSFileOriginalItemLocationKey=file:///personal/var/cellular/Containers/Shared/AppGroup/11C96DDE-7820-4053-AADE-4AAE1E7F3731/MyApp.sqlite, NSUnderlyingError=0x16da66a0 {Error Area=NSCocoaErrorDomain Code=513 “You don’t have permission to save lots of the file “MyApp.sqlite” within the folder “MyApp”.” UserInfo={NSURLKeysOfUnsetValuesKey=(
NSURLFileSecurityKey,
NSURLCreationDateKey
), NSFilePath=/personal/var/containers/Bundle/Utility/02E8B158-CE4A-4D3E-97A8-A04E19ED99A2/MyApp Watch App.app/MyApp.sqlite, NSUnderlyingError=0x16d92960 {Error Area=NSPOSIXErrorDomain Code=1 “Operation not permitted”}, NSURL=file:///personal/var/containers/Bundle/Utility/02E8B158-CE4A-4D3E-97A8-A04E19ED99A2/MyApppercent20Watchpercent20App.app/MyApp.sqlite}}, NSURL=file:///personal/var/cellular/Containers/Shared/AppGroup/11C96DDE-7820-4053-AADE-4AAE1E7F3731/MyApp.sqlite}

What can I do with it? Why I can not change sqlite file? Is there some other manner to do that?