菜鸟教程小白 发表于 2022-12-11 17:16:03

ios - iOS 10/macOS Sierra 中 Core Data 的 NSPersistentContainer


                                            <p><p>所以我现在<strong>真的</strong>很高兴能够使用 Core Data,因为他们让它<em>更简单</em>了(截至 WWDC 2016)。</p>

<p>在应用启动时,我计划从 Core Data 加载预先更新的数据,然后让 <code>persistentContainer</code> 的 <code>performBackgroundTask</code> 对某些实体进行更新/保存。由于实体在后台更新,因此 UI 在获取时应始终显示最新的<strong>最近</strong>数据(无论这些实体是否已更新)。</p>

<p>当我使用 ONE 上下文时,将 viewContext 的 <code>automaticallyMergesChangesFromParent</code> 标志设置为 <code>true</code> 并将其生成设置为 <code>.current</code> 是否安全>?</p>

<pre><code>lazy var persistentContainer: NSPersistentContainer = {

    let container: NSPersistentContainer = {
      $0.viewContext.automaticallyMergesChangesFromParent = true
      try? $0.viewContext.setQueryGenerationFrom(.current)
      return $0
    }(NSPersistentContainer(name: &#34;MyFirstApp&#34;))

    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
      if let error = error {
            fatalError(&#34;Unresolved error \(error), \(error._userInfo)&#34;)
      }
    })
    return container
}()
</code></pre>

<p>谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这两种设置都应该没问题,但如果您只使用一种上下文,则两者都无关紧要。 <code>automaticallyMergesChangesFromParent</code> 只会在不同的上下文执行保存时合并更改,并且当另一个上下文正在更改您当前上下文正在读取的数据时,查询生成很有用。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - iOS 10/macOS Sierra 中 Core Data 的 NSPersistentContainer,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38946462/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38946462/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - iOS 10/macOS Sierra 中 Core Data 的 NSPersistentContainer