菜鸟教程小白 发表于 2022-12-11 18:24:26

ios - 由于 NSManagedObjectContextObjectsDidChangeNotification 导致的运行时错误


                                            <p><p>我遇到了这个运行时错误,它在刷新数据或启动时(从服务获取并保存在 coreData)中发生 50% 的时间。我已经浏览了几乎所有关于这个问题的解决方案,但其中大多数都在 Obj-C 中,但我是 iOS 新手并且使用 swift。我有大约 20 个表,并且都使用相同的上下文。</p>

<p>下面是我的代码:</p>

<pre><code>public class ServiceCalls : NSManagedObject {
/*
             class func getContext () -&gt; NSManagedObjectContext {
      let appDelegate = UIApplication.shared.delegate as! AppDelegate

      let moc = NSManagedObjectContext(concurrencyType:.mainQueueConcurrencyType)
      let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
      privateMOC.parent = moc

      privateMOC.perform({
            do {
                try privateMOC.save()
            } catch {
                fatalError(&#34;Failure to save context: \(error)&#34;)
            }
      })
      return appDelegate.persistentContainer.viewContext
    }
             */ // tried this but didn&#39;t work

   class func getContext () -&gt; NSManagedObjectContext {
      let appDelegate = UIApplication.shared.delegate as! AppDelegate
      return appDelegate.persistentContainer.viewContext
    }
</code></pre>

<p>这就是我的使用方式,下面只是将数据保存在数据库中的一个示例。</p>

<pre><code>class func SaveCustomerContacts(name : String,id : String){

let context = getContext()
let entity =NSEntityDescription.entity(forEntityName: &#34;AllCustomerContacts_Tbl&#34;, in: context)
let newDoc = NSManagedObject(entity: entity!, insertInto: context)

newDoc.setValue(name, forKey: &#34;contactName&#34;)
newDoc.setValue(id, forKey: &#34;id&#34;)



    //save the object
do {
    try context.save()
    print(&#34;saved Customer contacts in Database yayy!&#34;)

} catch let error as NSError{
    print(&#34;Could not save \(error), \(error.userInfo)&#34;)
} catch {
    let nserror = error as NSError
    fatalError(&#34;Unresolved error \(nserror), \(nserror.userInfo)&#34;)
}
}
</code></pre>

<p>这是抛出的完整异常:</p>

<pre><code>    error: Serious application error.Exception was caught during Core Data change processing.This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.-: attempt to insert nil with userInfo (null)
CoreData: error: Serious application error.Exception was caught during Core Data change processing.This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.-: attempt to insert nil with userInfo (null)
2017-07-31 16:09:20.908 Sales CRM *** Terminating app due to uncaught exception &#39;NSInvalidArgumentException&#39;, reason: &#39;-: attempt to insert nil&#39;
*** First throw call stack:
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>错误表明正在插入一些 <code>nil</code> 值。</p>

<pre><code> error: Serious application error.Exception was caught during Core Data change processing.This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.-: attempt to insert nil with userInfo (null)
CoreData: error: Serious application error.Exception was caught during Core Data change processing.This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.-: attempt to insert nil with userInfo (null)
2017-07-31 16:09:20.908 Sales CRM *** Terminating app due to uncaught exception &#39;NSInvalidArgumentException&#39;, reason: &#39;-: attempt to insert nil&#39;
*** First throw call stack:
</code></pre>

<p>这个问题可能有多种原因:</p>

<ol>
<li>您正在使用多个上下文并且没有正确处理它们。</li>
<li>您正尝试在后台保存并发类型为 <code>MainQueueConcurrencyType</code> 的上下文。请改用 <code>PrivateQueueConcurrencyType</code>。</li>
<li>保存的值是 <code>nil</code> 或某种错误数据(在关系中)。</li>
</ol></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 由于 NSManagedObjectContextObjectsDidChangeNotification 导致的运行时错误,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45414169/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45414169/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 由于 NSManagedObjectContextObjectsDidChangeNotification 导致的运行时错误