简体   繁体   中英

App crash when saving a realm object… RLMAccessor set object to nil

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

saving an realm object must be inside the realm block.

try? realm.write {
  //realm add
  //realm update
  //realm delete
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM