App raise exception when trigger MOC save

Hello. I'm trying to use the primitive property to save the JSON data and then access the Codable object via custom getter and setter just likes this document: https://developer.apple.com/documentation/coredata/handling_different_data_types_in_core_data

But somehow my app raises crash sometimes when I trigger MOC save. The crash log says:

Fatal Exception: NSInvalidArgumentException
-[__NSCFData count]: unrecognized selector sent to instance 0x2899a1fa0

and the full log in this text file:

And it's hard to reproduce the same crash in DEBUG mode and I also have no idea how to trigger it for the release version. It just happens.

The app is open-source and the property declared here. The TwitterAttachment is a class adopt NSObject and Codable. And I could make some improvements:

  • add @NSManged private var primitiveAttachments: Data? rather than call setPrimitiveValue method
  • use defer for didChangeValue method call
  • or try the "transformable"…

But still no idea about the reason for the crash. Could someone help me find the issue about this crash or any hint about the pitfall about custom Core Data type persistent.

Thanks a lot.

App raise exception when trigger MOC save
 
 
Q