CoreData 无法加载/创建 NSPersistentContainer

问题描述 投票:0回答:1

我目前正在使用 CoreData(与 Apps Group)构建一个项目,以在我的键盘扩展和主应用程序之间共享数据。

我错误地删除了手机上的应用程序,自从我重新编译它以来,我在使用自定义键盘扩展时总是遇到此错误:

CoreData:错误::尝试从 addPersistentStore 期间遇到的错误中恢复:0x30120e280 错误域 = NSCocoaErrorDomain 代码 = 513 “无法保存文件,因为您没有授权。” UserInfo={reason=没有创建文件的权限;代码=1} SmartKeyboard/DataController.swift:37: 致命错误:未解决的错误 由于您没有授权,因此无法保存文件。,[“原因”:无权创建文件;代码 = 1].

这是我的代码:

class DataController: ObservableObject {
    enum SetScoreMethod {
        case decrement, increment
    }
    
    static var shared = DataController()
    var moc: NSManagedObjectContext
    @Published var game: Game?
    
    private init() {
        
        let container = NSPersistentContainer(name: "MODEL_NAME")
        if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "my.group.identifier")?.appendingPathComponent("MODEL_NAME.sqlite") {
        
            let description = NSPersistentStoreDescription(url: url)
            description.shouldInferMappingModelAutomatically = true
            description.shouldMigrateStoreAutomatically = true
            description.isReadOnly = false
            
            container.persistentStoreDescriptions = [description]
            container.loadPersistentStores { storeDescription, error in
                if let error = error as NSError? {
                    if FileManager.default.fileExists(atPath: url.path) {
                        try? FileManager.default.removeItem(at: url)
                    }
                    
                    fatalError("Unresolved error \(error.localizedDescription), \(error.userInfo)")
                }
            }
        }

        self.moc = container.viewContext
        fetchOrCreateGame()
    }
    
    func fetchOrCreateGame() -> Void {
        let fetchRequest: NSFetchRequest<Game> = Game.fetchRequest()
        do {
            let results = try self.moc.fetch(fetchRequest)
            if let existingGame = results.first {
                self.game = existingGame
            } else {
                let newGame = Game(context: self.moc)
                newGame.score = 0
                try self.moc.save()
                self.game = newGame
            }
        } catch {
            fatalError("Failed to fetch or create Game entity: \(error)")
        }
    }
    
    public func setScore(_ method: SetScoreMethod,with score: Int) -> Void {
        do {
            if method == .increment {
                self.game!.score += Int16(score)
            }
            
            if method == .decrement {
                if self.game!.score >= score {
                    self.game!.score -= Int16(score)
                }
            }
            
            try self.moc.save()
        } catch {
            fatalError("Failed to save new score: \(error)")
        }
        
    }
}

我需要一些解释来解决这个问题,我对 CoreData 不太了解

swift core-data nspersistentcontainer
1个回答
0
投票

这并不是专门的核心数据错误,而是关于文件权限以及 iOS 沙箱应用程序的方式。该错误告诉您不允许您的代码在某个位置创建文件。它显示在核心数据中,因为您的持久容器正在尝试在该位置创建 SQL 文件。

您的

url
很好并且应该可以工作——如果您的应用程序组权利设置正确。该错误表明此方法存在问题。可能应用程序或键盘扩展没有正确的权利,或者它们都有权利但不匹配。它们必须具有相同的应用程序组权限才能正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.