@@ -39,7 +39,7 @@ class RoomEngine(
3939
4040 val awareData = arrayListOf<AwareDataEntity >()
4141 data.forEach {
42- awareData.add(AwareDataEntity (data = AwareData (it, table)))
42+ awareData.add(AwareDataEntity (data = AwareData (it, table)))
4343 }
4444 db().AwareDataDao ().insertAll(awareData.toTypedArray())
4545 } catch (e: Exception ) {
@@ -53,7 +53,7 @@ class RoomEngine(
5353 return thread {
5454 try {
5555 val table = tableName ? : path
56- db().AwareDataDao ().insert(AwareDataEntity (id = id, data = AwareData (data, table)))
56+ db().AwareDataDao ().insert(AwareDataEntity (id = id, data = AwareData (data, table)))
5757 } catch (e: Exception ) {
5858 // TODO (sercant): user changed the password for the db. Handle it!
5959 e.printStackTrace()
@@ -139,7 +139,7 @@ class RoomEngine(
139139
140140 override fun run () {
141141 val entryCount = engine.db().AwareDataDao ().count(tableName)
142- val syncCount = if (entryCount > config.batchSize) entryCount / config.batchSize else 1
142+ val syncCount = if (entryCount > config.batchSize) entryCount / config.batchSize else 1
143143// Log.d("test", "Will sync table $tableName, $syncCount times.")
144144
145145 for (i in 0 .. syncCount) {
@@ -149,8 +149,11 @@ class RoomEngine(
149149
150150 if (data.isEmpty()) break
151151
152+ val combinedData = combineData(data)
153+ combinedData ? : continue
154+
152155 activeRequest.header(Pair (" Content-Type" , " application/json" ))
153- activeRequest.body(Gson ().toJson(data ))
156+ activeRequest.body(Gson ().toJson(combinedData ))
154157
155158 // waits for the response
156159 val (_, _, result) = activeRequest.responseString()
@@ -163,7 +166,7 @@ class RoomEngine(
163166 // TODO (sercant): log that there is something wrong.
164167 }
165168 }
166- },{
169+ }, {
167170 it.printStackTrace()
168171 })
169172 }
@@ -180,5 +183,18 @@ class RoomEngine(
180183 stopSync()
181184 super .interrupt()
182185 }
186+
187+ fun combineData (data : List <AwareData >): AwareData ? {
188+ if (data.isEmpty()) return null
189+
190+ val dataString = " [${data.joinToString { it.data }} ]"
191+
192+ return AwareData ().apply {
193+ this .timestamp = System .currentTimeMillis()
194+ this .tableName = data[0 ].tableName
195+ this .deviceId = data[0 ].deviceId
196+ this .data = dataString
197+ }
198+ }
183199 }
184200}
0 commit comments