https://kotlinlang.org logo
#android
Title
# android
k

Kashismails

07/30/2022, 1:43 PM
do you have any example of realm migration ? i have added a new entity and that requires realm to be migrated but i am unable to migrate my db. Secondly, if i use "delete realm if migration is needed" then on retrieving data the new entity is getting default values.
Copy code
open class RealmServiceImpl(private val realm: Realm) {
    fun insertSupplier(supplier: SupplierDomainModel) {

        CoroutineScope(Dispatchers.Default).async {
            write(supplier)
        }

    }

    fun insertPackedOrder(order: OrderDomainModel) {
        CoroutineScope(Dispatchers.Default).async {
            writeOrderPacked(order)
        }
    }

    private suspend fun writeOrderPacked(order: OrderDomainModel) =
        realm.write {
            copyToRealm(
                order.asOrderEntity()
            )
        }

    private suspend fun write(supplier: SupplierDomainModel) =
        realm.write {
           copyToRealm(
                supplier.asEntity()
            )
        }

    private fun delete() {
        realm.writeBlocking {
            val writeTransactionTasks = realm.query<SupplierEntity>().find()
            delete(writeTransactionTasks)
        }
    }

    fun removePackedOrder(order: OrderDomainModel) {
        CoroutineScope(Dispatchers.Default).launch {
            removeOrder(order)
        }
    }

    private suspend fun removeOrder(order: OrderDomainModel) {
        realm.write {
            val deletable = this.query<OrderEntity>("orderId == ${order.orderItemId}").find().first()
            delete(deletable)
        }
    }

    fun getPackedOrders() = realm.query<OrderEntity>().find()

    fun getSupplier() = realm.query<SupplierEntity>().find().last()

}
this is my realm class supplier's data is getting retrieved successfully
Copy code
suspend fun getShippingBookedOrders(): DataState<List<OrdersDTO>> {
    return withContext(Dispatchers.Default + SupervisorJob()) {
        val ktor = async {
            ktorService.getShippingBookedOrders(supplierId = supplier!!.sid)
        }

        val ktorServiceList = ktor.await()
        val ordersList =realmServiceImpl.getPackedOrders()

        ktorServiceList.data?.asDomainModel()?.map { item ->
            item.isPacked = ordersList.any {
                item.orderItemId == it.orderId
            }
        }

        ktorServiceList
    }
}
open class OrderEntity: RealmObject {
   open var orderId: Int = -130
}
🧵 3
90 Views