gpt4 book ai didi

ios - 无法让 NSManagedObject.automaticallyMergesChangesFromParent 工作

转载 作者:行者123 更新时间:2023-12-02 01:42:20 30 4
gpt4 key购买 nike

我重写了我们的 iOS 应用程序,以使用 Core Data 中的新 iOS 10 功能。特别是,我已将核心数据堆栈创建切换到新的 NSPercientStore 设置。

我们已经有了一个主队列上下文和一个后台上下文,因此我们直接使用来自 NSPercientStore 对象的那些上下文。我还从使用通知合并跨上下文的更改更改为在 viewContext 和 backgroundContext 中设置新的automaticallyMergesChangesFromParent。

不幸的是,我现在在更新 UICollectionViewController 和 View 的 NSFetchedResultsController 中遇到了一些奇怪的崩溃。

因此,我设置了“-com.apple.CoreData.ConcurrencyDebug 1”启动参数,以确保事情发生在正确的队列上,并且当后台线程尝试保存后台对象上下文时,我会立即收到异常,即使堆栈跟踪显示“正确”后台队列上发生的异常:

#0  0x00000001948b89bc in +[NSManagedObjectContext __Multithreading_Violation_AllThatIsLeftToUsIsHonor__] ()
#1 0x0000000194853724 in -[_PFBatchFaultingArray managedObjectIDAtIndex:] ()
#2 0x0000000194853600 in -[_PFMutableProxyArray newArrayFromObjectIDs] ()
#3 0x0000000194853548 in -[_PFMutableProxyArray arrayFromObjectIDs] ()
#4 0x0000000194852638 in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidSave:] ()
#5 0x00000001924e622c in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
#6 0x00000001924e5930 in _CFXRegistrationPost ()
#7 0x00000001924e56ac in ___CFXNotificationPost_block_invoke ()
#8 0x0000000192554b9c in -[_CFXNotificationRegistrar find:object:observer:enumerator:] ()
#9 0x0000000192427bf4 in _CFXNotificationPost ()
#10 0x0000000192f2e6bc in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#11 0x00000001948c5b48 in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postContextDidSaveNotificationWithUserInfo:] ()
#12 0x0000000194850350 in -[NSManagedObjectContext(_NSInternalAdditions) _didSaveChanges] ()
#13 0x000000019483ca0c in -[NSManagedObjectContext save:] ()

关于我可能做错了什么有什么想法吗?以下是一些相关代码:

var initializationError: NSError?

// The persistent container (local database) for the application.
lazy var persistentContainer: NSPersistentContainer = {

let container = NSPersistentContainer(name: self.storeName)
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
self.initializationError = error // This will be displayed by the AppDelegate
}
})
return container
}()

// The managed object context for the main thread of the application.
lazy var mainObjectContext: NSManagedObjectContext? = {

// Get the main thread context.
let context = self.persistentContainer.viewContext
context.mergePolicy = NSOverwriteMergePolicy // In-memory properties overwrite datastore properties
context.automaticallyMergesChangesFromParent = true // Merge all changes.

return context
}()

// Return a managed object for background threads to create and modify objects without affecting the main thread until necessary.
lazy var backgroundObjectContext: NSManagedObjectContext? = {

// Get a background thread context.
let context = self.persistentContainer.newBackgroundContext()
context.mergePolicy = NSOverwriteMergePolicy // In-memory properties overwrite datastore properties
context.automaticallyMergesChangesFromParent = true // Merge all changes.

return context

}()

// Synchronously save any pending object changes in a managed object context to the database persistent store.
class func saveAnyChangesSync(_ context: NSManagedObjectContext) -> NSError? {

var error: NSError?

// If the context has changes, save them.
if context.hasChanges {

do {
try context.save()

} catch let saveError as NSError {
error = saveError
NSLog("CoreData error saving context \(saveError)")
}
}
return error
}

最佳答案

如果您在 Swift 中管理核心数据,并且没有看到后台保存和主上下文之间传播的更改,那么您(像我一样)可能会缺少 autoreleasepool 部分(pfffbt!objective-c)。在进行此更改之前,我看到了一堆错误,尽管我的错误都是关于 NSSet 发生变异。

您可能还想查看合并策略 - 我认为两个上下文不应该具有相同的策略。主要应该屈服于文件存储,而后台应该坚持当前对象的更改。

缩写示例:

// When initializing:
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
persistentContainer.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyStoreTrump
// And when saving:
persistentContainer.performBackgroundTask { moc in
moc.automaticallyMergesChangesFromParent = true
moc.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
autoreleasepool {
// ... do some nsmanagedobject loading/changing
}
do {
try moc.save()
} catch {}
}

关于ios - 无法让 NSManagedObject.automaticallyMergesChangesFromParent 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40065550/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com