송규빈
송규빈

Reputation: 75

How to use RemoteMediator when the server does not provide an ID value

@Serializable
data class InfoResponse(
    val totalCount: Int?,
    val pageableCount: Int?,
    val isEnd: Boolean?
)

@Serializable
data class ItemResponse(
    val info: InfoResponse?,
    val items: List<ItemDocumentResponse>?,
) {
    @Serializable
    data class ItemDocumentResponse(
        val collection: String?,
        val imageUrl: String?,
        val datetime: String?,
    )
}

The response from the server is as above.

I am using a remote Mediator, but the server does not provide a page value or a unique value (id) for each item.

So, I tried to create and manage remoteDao for the previous page and next key, but I don't know how to set the remoteKey's ID value.

@Entity(tableName = "remote_keys")
data class RemoteKey(
    @PrimaryKey val id: Int = 0,
    val prevKey: Int?,
    val nextKey: Int?,
)        

       database.withTransaction {
                if (loadType == LoadType.REFRESH) {
                    database.remoteKeysDao().clearRemoteKeys()
                    database.itemsDao().clearItems()
                }
                val nextKey = page + (if (response.isEnd) 0 else 1)
                database.remoteKeysDao().insert(RemoteKeys(item.id, nextKey, response.isEnd))
                database.itemsDao().insertAll(response.items)
            }

Looking at other examples, it seems that item.id uses remoteKey as above, but in my case, I cannot determine the unique value of each item from the server response.

Upvotes: 2

Views: 35

Answers (0)

Related Questions