如何以正确的方式加载PersistentStores?
整个周末和今天,我一直在互联网上查看如何正确处理persistentStores加载时正确处理任何错误,而无需成功!
实际上,如此多的人正在寻找相同的解决方案,但没有人得到答案。例如: https:/ /swift/core-data-error-checking/3794
苹果在模板中给我们一个fatalerror
,同时告诉我们不要将其用于运输申请。
例如,从YouTube上的许多教程中,您也可以使用它:
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {
print("Core Data failed to load: \(error.localizedDescription)!")
}
print("Core Data succeed to load!")
})
因为我无法处理潜在的错误,因为我仍然不知道该怎么做,我很想只加载它处理任何错误;但是,如果和print
,我不能仅删除,然后将其留为空白。那么,我应该放什么呢?
I've been looking on Internet the entire weekend and today on how to handle correctly any error when the PersistentStores loads, without a success!
Actually, so many people are looking for the same solution but no one gets an answer. For example: https://www.hackingwithswift.com/forums/swift/core-data-error-checking/3794
Apple give us a fatalError
in their template while telling us to not use it for a shipping application.
Here's also how you can have it, from many tutorials on Youtube, for example:
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {
print("Core Data failed to load: \(error.localizedDescription)!")
}
print("Core Data succeed to load!")
})
As I'm not able to handle a potential error, because I still don't know how to do it, I'd love to just load it without handling any error ; but I can't just delete the if
and the print
and leave it blank. So, what should I put inside?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题没有直接的答案,因为您的工作取决于您的应用程序的作用以及在这种情况下它如何响应。每个应用程序的要求都不同,您没有给您任何迹象。
如果发生此错误,您的应用程序未能加载持久存储。该商店中可能没有任何数据。新数据不能保存到该商店。你该怎么办?您在应用程序中可以做什么来满足您的要求? 如果该持久性商店不可用,您希望您的应用程序会做什么?您认为在特定应用中,您认为什么是合理的响应?那就是你在这里做的。
没有真正的恢复路径;您不能简单地将错误消失。您可以尝试删除旧的持久商店并创建一个新商店。那将失去任何以前保存的数据。可以吗?这取决于您和您的应用。如果要这样做,请查看
DestroyperSistentStore(at:,type:,option:)
函数以干净地删除它。然后尝试再次加载商店,并将创建一个新的商店。The question doesn't have a straightforward answer because what you do depends on what your app does and how you want it to respond in this situation. Every app's requirements are different, and you have not given any indication of yours.
If this error happens, your app has failed to load the persistent store. Whatever data might have been in that store is not available. New data cannot be saved to that store. What should you do? What can you do, in your app, to meet your requirements? What do you want your app to do if this persistent store is not available? What would you consider to be a reasonable response, in your specific app? That is what you do here.
There's no real recovery path; you can't simply make the error go away. You could try deleting the old persistent store and creating a new one. That would lose any previously saved data. Is that OK? It depends on you and your app. If you want to do that, look into the
destroyPersistentStore(at:, type:, options:)
function to remove it cleanly. Then try loading the store again, and a new one will be created.