如何解决罕见情况下的核心数据随机删除一切
很少(
有一次,用户能够通过关闭和打开我的应用程序的 iCloud 开关(在手机设置中)来恢复数据。但是,这对其他用户不起作用。
我读到 iOS 可能会在存储空间已满时删除缓存。但是,据我了解,容器的默认位置是在应用程序支持中。
这是我在 App Delegate 中的核心数据实现:
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentCloudKitContainer(name: "Bench")
container.loadPersistentStores(completionHandler: { (storeDescription,error) in
if let error = error as NSError? {
print("Unresolved error \(error),\(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
return container
}()
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
context.perform {
do {
try context.save()
} catch {
let nserror = error as NSError
print(nserror.localizedDescription)
}
}
}
}
我像这样全局访问它:
let ad = UIApplication.shared.delegate as! AppDelegate
let context = ad.persistentContainer.viewContext
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。