I have a transient optional attribute addedImages in Swift as part of an one-to-many relationship within a managed object with the attribute and the accessor methods:
我在Swift中有一个瞬态可选属性addedImages,作为托管对象中具有属性和访问器方法的一对多关系的一部分:
…
@NSManaged public var addedImages: NSSet?
…
@objc(addAddedImagesObject:)
@NSManaged public func addToAddedImages(_ value: MMImage)
@objc(removeAddedImagesObject:)
@NSManaged public func removeFromAddedImages(_ value: MMImage)
@objc(addAddedImages:)
@NSManaged public func addToAddedImages(_ values: NSSet)
@objc(removeAddedImages:)
@NSManaged public func removeFromAddedImages(_ values: NSSet)
…
The failing code is run within a serial queue and crashes with an EXC_BAD_ACCESS error.
失败的代码在串行队列中运行,并因EXC_BAD_ACCESS错误而崩溃。
let backgroundContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)
backgroundContext.performAndWait {
print("\(self.addedImages?.count ?? 0) added images")
self.addToAddedImages(image)
}
It works without issues when I rather define addedImages as non-transient. I'm not sure if this is a normal behavior, and how I could fix this. Thank you for your help.
当我将addImages定义为非瞬态时,它没有问题。我不确定这是否是正常行为,以及我如何解决这个问题。谢谢您的帮助。
1 个解决方案
#1
0
My assumption that a transient attribute would be initialized with nil during the creation and fetch was wrong. When I tried to access count, the object was simply not in memory. I had to add this to awakeFromFetch and awakeFromInsert, which works.
我假设在创建和获取期间使用nil初始化transient属性是错误的。当我试图访问count时,对象根本就不在内存中。我不得不将它添加到awakeFromFetch和awakeFromInsert,它可以工作。
if self.addedImages == nil {
self.addedImages = nil
}
#1
0
My assumption that a transient attribute would be initialized with nil during the creation and fetch was wrong. When I tried to access count, the object was simply not in memory. I had to add this to awakeFromFetch and awakeFromInsert, which works.
我假设在创建和获取期间使用nil初始化transient属性是错误的。当我试图访问count时,对象根本就不在内存中。我不得不将它添加到awakeFromFetch和awakeFromInsert,它可以工作。
if self.addedImages == nil {
self.addedImages = nil
}