CoreData 崩溃 NSTaggedDate objectForKey

CoreData crash NSTaggedDate objectForKey

提问人:YosiFZ 提问时间:9/6/2023 更新时间:9/6/2023 访问量:42

问:

我在我的 iOS 应用程序中使用 CoreData,我有一个函数可以为每个线程创建 NSManagedObjectContext:

var contextDict = [Thread:NSManagedObjectContext]()

var context:NSManagedObjectContext{
    if let context = contextDict[Thread.current]{
        return context
    }else{
        let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
        context.persistentStoreCoordinator = persistentStoreCoordinator
        contextDict[Thread.current] = context
        return context
    }
}

在初始化中,我使用以下命令创建主初始化:

contextDict[Thread.main] = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)

突然间,我开始在这种方法上崩溃(并非一直如此):

if let context = contextDict[Thread.current]

崩溃日志:

-[__NSTaggedDate objectForKey:]: unrecognized selector sent to instance 0x8000000000000000
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSTaggedDate objectForKey:]: unrecognized selector sent to instance 0x8000000000000000'
*** First throw call stack:
(0x1b05b2d94 0x1a966c3d0 0x1b0727b14 0x1b05c91c8 0x1b062f9f0 0x1aa3c0b88 0x107919d00 0x10791c344 0x10790c220 0x10790ced0 0x107914bd0 0x107905814 0x1070ca3d8 0x107024e84 0x10799c520 0x10799e038 0x1079a60b0 0x1079a6df4 0x1079b3c74 0x20ff33ddc 0x20ff33b7c)
libc++abi: terminating due to uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSTaggedDate objectForKey:]: unrecognized selector sent to instance 0x8000000000000000'
terminating due to uncaught exception of type NSException

知道问题可以做什么吗?

IOS版 swift Objective-C Core-Data

评论

0赞 Larme 9/6/2023
似乎是一个.为什么?这是一个很好的问题,它缺少你如何真正保持物体的活力。但目前尚不清楚你为什么要这样做。最好在线程内创建上下文,这样就可以了,并且始终使用 / 以确保在该线程中执行。但是,如果您从另一个线程访问,则可能存在一些并发访问问题。contextDictDateperform()performAndWait()contextDict

答: 暂无答案