Quantcast
Channel: Mobile - Couchbase Forums
Viewing all articles
Browse latest Browse all 1116

Must be called during a transaction (CouchbaseLite Android v3.1.0-425@33 (EE/release, Commit/3f009a25f8@934af6e32f2a Core/3.1.0 (356) at 2023-04-14T04:06:14.503123Z) on Java; Android 12; SM-T500)

$
0
0

Sometimes we are facing this issue, as it gets triggered during the data-saving process into the database, but the data is not being saved. We encounter the following error:

Note: We are unable to catch the error all the time, but errors are being recorded in production, and data is not being saved.

Couchbase version: “com.couchbase.lite:couchbase-lite-android-ee:3.1.0”

Error: “must be called during a transaction”
(CouchbaseLite Android v3.1.0-425@33 (EE/release, Commit/3f009a25f8@934af6e32f2a Core/3.1.0 (356) at 2023-04-14T04:06:14.503123Z) on Java; Android 12; SM-T500)

Code:
return withContext(backgroundDispatcher) {

        val queryResult = kotlin.runCatching {

            val map = converter.toMap(model).toMutableMap().apply {
                this.remove("_id")
            }
            val document = MutableDocument(id, map)
            
            cbHelper.getDataBase().defaultCollection?.save(
                document
            ) { newDoc, curDoc ->
                if (curDoc == null) {
                    // No existing document, treat it as creation
                    true
                } else {
                    val resolvedDoc = MutableDocument(newDoc.id)

                    // Remove the _id key from newDoc before merging
                    val newDataMap: MutableMap<String, Any> = newDoc.toMap().toMutableMap()
                    newDataMap.remove("_id")

                    // Merge the data from newDoc and curDoc

                    val mergedDataMap: MutableMap<String, Any> = mutableMapOf()
                    mergedDataMap.putAll(curDoc.toMap())
                    mergedDataMap.putAll(newDataMap)
                    resolvedDoc.setData(mergedDataMap)
                    true
                }
            }
            

            document.id
        }

        queryResult.getOrNull()
            ?.let { docId -> getDocument(docId) }
            ?: kotlin.run {
                val error =queryResult.exceptionOrNull()!!
                Log.d("Error Save/Update",error.toString())
                QueryResult.error(error)
            }
    }

Please help us to fix the error

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 1116

Trending Articles