菜鸟教程小白 发表于 2022-12-12 15:00:30

ios - 删除所有核心数据记录在managedContext中没有设置hasChanges


                                            <p><p>我是 coreData 的新手,遇到一个问题:</p>

<p>我的应用程序执行以下 3 个连续的核心数据功能:</p>

<pre><code>    let managedContext = persistentContainer.viewContext
    deleteAllCDRecords(managedContext: managedContext, in: &#34;CDShoppingItem&#34;)
    saveManagedContext(managedContext: managedContext)
</code></pre>

<p>它们被定义(缩短)为:</p>

<pre><code>private func deleteAllCDRecords(managedContext: NSManagedObjectContext, in entity: String) {
    let deleteFetch = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: entity)
    let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
    do {
      try managedContext.execute(deleteRequest)
    } catch let error as NSError {
      // error handling
    }
} // deleteAllCDRecords
</code></pre>

<p>和</p>

<pre><code>private func saveManagedContext(managedContext: NSManagedObjectContext) {
    if !managedContext.hasChanges { return }
    do {
      try managedContext.save()
    } catch let error as NSError {
      // error handling
    }
} // saveManagedContext
</code></pre>

<p><strong>问题:</strong> </p>

<p><code>deleteAllCDRecords</code>执行后,<code>saveManagedContext</code>函数中的<code>managedContext.hasChanges</code> <strong>不成立</strong>,因此删除是未保存到持久存储中。 </p>

<p><strong>我的问题:</strong><br/>
我的代码有什么问题?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>批量删除操作在持久存储本身中进行。因此,在这种特殊情况下,您从持久存储中删除实体,然后必须删除内存中的对象。</p>

<blockquote>
<p>Batch deletes run faster than deleting the Core Data entities yourself
in code because they operate in the persistent store itself, at the
SQL level. As part of this difference, the changes enacted on the
persistent store are not reflected in the objects that are currently
in memory.</p>

<p>After a batch delete has been executed, remove any objects in memory
that have been deleted from the persistent store.</p>
</blockquote>

<p>见 <a href="https://developer.apple.com/library/archive/featuredarticles/CoreData_Batch_Guide/BatchDeletes/BatchDeletes.html" rel="noreferrer noopener nofollow">https://developer.apple.com/library/archive/featuredarticles/CoreData_Batch_Guide/BatchDeletes/BatchDeletes.html</a> .</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 删除所有核心数据记录在managedContext中没有设置hasChanges,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54296652/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54296652/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 删除所有核心数据记录在managedContext中没有设置hasChanges