Reputation: 981
I am getting a crash whenever I save a realm object, the crash point to a property of type SaleItemVariation which sets nil from RLMAccessor.mm
.
if (_promote_existing && [obj isKindOfClass:_info.rlmObjectSchema.objectClass] && !prop.swiftIvar) {
// set the ivars for object and array properties to nil as otherwise the
// accessors retain objects that are no longer accessible via the properties
// this is mainly an issue when the object graph being added has cycles,
// as it's not obvious that the user has to set the *ivars* to nil to
// avoid leaking memory
if (prop.type == RLMPropertyTypeObject) {
((void(*)(id, SEL, id))objc_msgSend)(obj, prop.setterSel, nil);
}
}
My classes:
final class SaleItem: Object, Mappable{
.....
@objc dynamic var variation: SaleItemVariation!
......
var singleQuantityPrice: Double {
return variation.price // Crash here, Fatal error: Unexpectedly found nil while unwrapping an Optional value
}
}
class SaleItemVariation: Object, Mappable{
@objc dynamic var price: Double = 0
}
I don't have any idea why is this happening?
Note: Realm version is 2.10.2
Upvotes: 3
Views: 549
Reputation: 493
saving an realm object must be inside the realm block.
try? realm.write {
//realm add
//realm update
//realm delete
}
Upvotes: 1