如何修复尝试使用 coredata 递归调用 -save 错误?

Posted

技术标签:

【中文标题】如何修复尝试使用 coredata 递归调用 -save 错误?【英文标题】:how to fix attempt to recursively call -save error with coredata? 【发布时间】:2017-07-07 05:27:41 【问题描述】:

我在保存核心数据时随机收到此错误

Unresolved error Error Domain=NSCocoaErrorDomain Code=132001 "(null)" UserInfo=message=attempt to recursively call -save: on the context aborted, stack trace=(

过去 3 个月一切正常,但最近由于应用程序的更改,我不得不调用大量获取和保存请求,其中一些处于循环中,而在我面临这些更改后,一些处于关闭状态这个错误。

这是核心数据管理器的代码

import Foundation
import CoreData
class CoreDataStack 
    private init() 

    

    class func getContext () -> NSManagedObjectContext 
        return CoreDataStack.managedObjectContext
    
    // MARK: - Core Data stack

    static var managedObjectContext: NSManagedObjectContext = 

        var applicationDocumentsDirectory: URL = 
            // The directory the application uses to store the Core Data store file. This code uses a directory named "com.cadiridris.coreDataTemplate" in the application's documents Application Support directory.
            let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
            return urls[urls.count-1]
        ()

        var managedObjectModel: NSManagedObjectModel = 
            // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
            let modelURL = Bundle.main.url(forResource: "Thyssenkrupp", withExtension: "momd")!
            return NSManagedObjectModel(contentsOf: modelURL)!
        ()

        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: managedObjectModel)
            let url = applicationDocumentsDirectory.appendingPathComponent("Thyssenkrupp.sqlite")
            var failureReason = "There was an error creating or loading the application's saved data."
            let options = [ NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption:true ]
            do 
                try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
             catch 
                // Report any error we got.
                var dict = [String: AnyObject]()
                dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
                dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?

                dict[NSUnderlyingErrorKey] = error as NSError
                let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", 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.
                print("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
                //abort()
            

            return coordinator
        ()

        // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
        let coordinator = persistentStoreCoordinator
        var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
        managedObjectContext.persistentStoreCoordinator = coordinator
        return managedObjectContext
    ()

    // MARK: - Core Data Saving support

    class func saveContext () 
        DispatchQueue.main.async  
            if managedObjectContext.hasChanges 
                do 
                    try managedObjectContext.save()
                 catch 
                    // Replace this implementation 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.
                    let nserror = error as NSError
                    print("Unresolved error \(nserror), \(nserror.userInfo)")
                    //abort()
                
            
        

    

请提供任何建议为什么会出现此错误

【问题讨论】:

使用 managedObjectContext 的 performBlock 或 performBlockAndWait 切换到正确的队列。显然在这种情况下它不会有太大的区别,因为你的 managedObjectContext 与 mainQueue 相关联,你正在切换到主队列,但是如果你使用队列限制并发策略,你应该避免手动切换。 @SandeepBhandari 你有什么快速解决方法或任何文章可以帮助我解决问题吗? 我知道这已经有一段时间了,但是你有没有想过如何解决这个错误? @squarehippo10 我不记得它是否已解决,但我认为问题在于过于频繁地保存核心数据,例如循环保存,因此请避免这种情况并检查是否对您有帮助 谢谢!循环保存正是我正在做的事情。它在一段时间内运行良好,但我想我需要进行一些更改。 【参考方案1】:

问题是经常将数据保存到 CoreData,是的,您可以随心所欲地使用 CoreData,但是如果您添加/删除/更新数据并将其保存在循环中,它会在控制台上出现此错误,这样做会导致这个错误,并非总是如此,但最好在循环完成后保存 CoreData。由于保存到核心数据在我们执行创建、更新、删除操作的情况下很重要,并且我们没有保存 CoreData 应用程序由于某种原因崩溃/关闭,那么数据将从我们上次保存 CoreData 的点开始丢失。保存到 CoreData 就像一个检查点,一切都被保存了。所以循环保存不是有效的方法。

【讨论】:

以上是关于如何修复尝试使用 coredata 递归调用 -save 错误?的主要内容,如果未能解决你的问题,请参考以下文章

iOS Swift 3核心数据-尝试递归调用-save的致命错误:上下文中止

CoreData multithreading_violation 调试

C#:递归方法期间的堆栈溢出异常

如何修复“尝试调用方法‘addMoney’(零值)”错误?

CoreData NSOrderedSet - 修复?

OutOfMemoryError:Java 堆空间。如何修复递归方法中发生的这个错误?