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)
}