I want to update a RealmObject that contains RealmList and I faced this error : (look at the images)
So why I faced this error? and what's the solution?
Also why RealmObject has duplicate parameters (image 2)
My code and the error are below
InvoiceModel :
open class PurchaseDatabase : RealmObject {
@PrimaryKey
var id: Int? = 0
var date: String? = ""
var supplier: String? = ""
var products: RealmList<ProductBuyModel> = realmListOf()
var totalPrice: Int? = 0
var paymentMethod: String? = ""
var paymentPrice: Int? = 0
var restPrice: Int? = 0
}
Error :
FATAL EXCEPTION: main
Process: com.bakirdev.gestiondestock, PID: 9705
java.lang.IllegalArgumentException: Cannot import an outdated object. Use findLatest(object) to find an
up-to-date version of the object in the given context before importing it.
at io.realm.kotlin.internal.RealmObjectListOperator.insert(RealmListInternal.k:370)
at io.realm.kotlin.internal.ListOperator$DefaultImpls.insertAll(RealmListInternal.kt:202)
at io.realm.kotlin.internal.BaseRealmObjectListOperator.insertAll(RealmListInternal.kt:258)
at com.bakirdev.gestiondestock.ui.purchases.PurchaseDatabase.setProducts(PurchaseDatabase.kt:150)
at com.bakirdev.gestiondestock.ui.purchases.AddNewInvoice$initListenerEdit$2$1.invoke(AddNewInvoice.kt:168)
at com.bakirdev.gestiondestock.ui.purchases.AddNewInvoice$initListenerEdit$2$1.invoke(AddNewInvoice.kt:162)
at io.realm.kotlin.internal.SuspendableWriter$write$2.invokeSuspend(SuspendableWriter.kt:109)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.os.HandlerThread.run(HandlerThread.java:67)
My Code :
var invoiceModel: PurchaseDatabase? = purchasesList[position]
realm.writeBlocking {
// fetch a frog from the realm by primary key
invoiceModel = this.query<PurchaseDatabase>("id == ${model.id}").first().find()
// modify the frog's age in the write transaction to persist the new age to the realm
invoiceModel?.supplier = purchasesSupplier
invoiceModel?.products?.clear()
invoiceModel?.products = products
invoiceModel?.totalPrice = totalPrice
invoiceModel?.paymentMethod = paymentMethod
invoiceModel?.paymentPrice = purchasesPaymentPrice.trim().toInt()
invoiceModel?.restPrice = purchasesRestPrice.trim().toInt()
}
Purchases.invoiceModel = invoiceModel
val returnedValue = Intent().apply {
putExtra("position", position)
}
setResult(RESULT_OK, returnedValue)
finish()
