Data not getting with query while first time install app android?

Below is my code;

 val query = database?.createAllDocumentsQuery()
    query?.startKey = "refill-device-SERIAL-AB121-0"
    query?.endKey = "refill-device-SERIAL-AB121-7"
    var result: QueryEnumerator? = null
    try {
        result = query?.run()
    } catch (e: CouchbaseLiteException) {
        e.printStackTrace()
    }

    val it = result
    while (it?.hasNext()==true) {
        val row = it.next()
        if (row.documentId.startsWith("refill-device-SERIAL-")) {
            val obj = JSONObject(row.document.properties)
            val canisters = gson.fromJson(obj.toString(), Canisters::class.java)
            arrayList.add(canisters)
        }
    }
    canistersArrayList.postValue(arrayList)

I am using MVVM pattern and this query gives me random documents while first time install in mobile. How can I pull all documents using replication ?

class DocumentsObserveViewModel(application: Application) : AndroidViewModel(application) {
private var database: Database? = null
private var pull: Replication? = null
private var obj: JSONObject? = null
private var db_id: String? = null
private var couch_db_url: String? = null
val progressBarVisibility: MutableLiveData<Int> = MutableLiveData()
var refillDevicesList = MutableLiveData<List<RefillDeviceListModel.Device>>()
var canistersArrayList = MutableLiveData<MutableList<Canisters>>()
var gson = Gson()
private val arrayList = ArrayList<Canisters>()
lateinit var refillDeviceListModel: RefillDeviceListModel
private val context = ApplicationClass.instance

init {
    db_id = SharedPrefUtil<String>()[ApplicationClass.instance, SharedPrefs.DATABASE_ID.key, ""].toString()
    if (database == null) {
        database = DBConnector.inializeDb(context, "company-$db_id")
        couch_db_url = SharedPrefUtil<String>()[ApplicationClass.instance, Constants.COUCH_DB_URL, ""].toString()
        pull = DBConnector.getPullReplecation(database, couch_db_url.plus("/company-").plus(db_id))
        pull?.addChangeListener { event ->
            if (pull?.status == Replication.ReplicationStatus.REPLICATION_IDLE && event.completedChangeCount ==event.changeCount) {
                getRefillDevicesForFirstTime()
            }
        }
    }
    getCanisterForFirstTime()
    getRefillDeviceListChanges()
    getCanisterDataChanges()
}


private fun getRefillDevicesForFirstTime() {
    progressBarVisibility.postValue(1)
    val obj = JSONObject(database?.getDocument("refill-device")?.properties)
    val gson = Gson()
    refillDeviceListModel = gson.fromJson<RefillDeviceListModel>(
        obj.toString(),
        RefillDeviceListModel::class.java
    )
    refillDevicesList.postValue(refillDeviceListModel.data?.devices)
    progressBarVisibility.postValue(0)
}

The documentation you need is here: https://docs.couchbase.com/couchbase-lite/2.7/java-android.html#replication

I had a quick look at the code and there, really, is no way that I’m going to be able to debug this for you, or comment on it. It is a small snippet of a complex system.
Your best bet is to follow the documentation and ask specific questions if you encounter problems.

btw: what is SyncStateContract.Constants.COUCH_DB_URL? Not its value; how is it defined?

I am using MVVM pattern and this query gives me random documents while first time install in mobile.

I don’t know what you mean by “random documents”. Do you mean “only some of the documents”? Replication is asynchronous, so you’re going to be seeing documents appear in the database during replication until it’s complete.

I don’t see anything broken about that first code snippet; it’s just inefficient because it’s converting the document back to JSON and then re-parsing it.

I don’t understand your second snippet because it’s using a lot of custom(?) classes like MutableLiveData and DBConnector.

Can you please describe specifically what you’re doing, what you expect to happen, and what actually happens?

And note again that CBL 1.x is ancient, few of us here even remember how the API worked, and we’re about to stop supporting it in another month or so.

Hii @jens How can I wait for the pull all documents till retrieving by replication ? and then can do another code for viewing data and all because issue is only that not all documents pull properly and I navigate to another screen so viewmodel recreate. So is there any way to wait for the pull documents by replication and that code only one time execute. I dont want to execute that code all time while changes in document

Start a one-shot (non-continuous) replication, add a listener, then watch for its state changing to “stopped”. At that point the replicator is done.