菜鸟教程小白 发表于 2022-12-11 17:18:34

ios - 核心数据 NSPersistentStoreCoordinator 中止来电,SWIFT


                                            <p><p>我花了很多时间让我的 Core Data 模型有效地为我正在开发的 iOS 应用程序工作。在大多数情况下,我的一切工作都很顺利,但前几天我在加载数据时遇到了一个独特的错误,这只是偶然。我刚开始在我的 iPhone 上运行我的应用程序进行测试,就在它启动并开始使用 Core Data 加载数据时,我姐姐调用我。时机相当偶然,因为该中断似乎从 <code>AppDelegate</code> 中的 <code>persistentStoreCoordinator</code> 调用了 <code>abort()</code> 函数。</p>

<p>这是我所指的代码:</p>

<pre><code>lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
    // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
    // Create the coordinator and store
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(&#34;DataModel.sqlite&#34;)
    var failureReason = &#34;There was an error creating or loading the application&#39;s saved data.&#34;
    let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
      NSInferMappingModelAutomaticallyOption: true]
    do {
      try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions)
    } catch {
      // Report any error we got.
      var dict = ()
      dict = &#34;Failed to initialize the application&#39;s saved data&#34;
      dict = failureReason

      dict = error as NSError
      let wrappedError = NSError(domain: &#34;YOUR_ERROR_DOMAIN&#34;, code: 9999, userInfo: dict)
      // Replace this with code to handle the error appropriately.
      // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
      NSLog(&#34;Unresolved error \(wrappedError), \(wrappedError.userInfo)&#34;)

      abort()// THIS
    }

    return coordinator
}()
</code></pre>

<p>显然,我看到 Apple 在哪里评论说 <code>abort()</code> 函数只需要在开发期间使用,并且应该在生产中删除一次,但我想知道是否有人可以告诉我如何正确处理此类错误? </p>

<p>我没有加载大量数据,我想在这种情况下我需要做的就是重新开始加载过程。</p>

<p>我想我真正要问的是,一旦发现错误,是否有简单的方法告诉它重试,还是我必须手动返回到我在应用启动时调用数据获取的方式?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以重试,但<strong>通常</strong>没有意义。如果添加持久存储失败,<strong>通常</strong>没有机会再加载它,因为数据已损坏或无法读取。剩下的唯一选择是删除现有文件,创建一个新文件,然后(如果可用)从您可能使用的任何云服务中恢复数据。</p>

<p>但是,如果您认为值得再试一次。使用相同的参数再次调用 <code>addPersistentStoreWithType</code> 并查看它是否有效。检查返回值——如果它不是 nil 并且没有抛出错误,那么你就可以开始了。在您从这个惰性 var 初始化返回之前,您可以根据需要多次调用该方法。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 核心数据 NSPersistentStoreCoordinator 中止来电,SWIFT,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39048237/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39048237/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 核心数据 NSPersistentStoreCoordinator 中止来电,SWIFT