@@ -38,6 +38,7 @@ import io.getstream.chat.android.client.internal.state.plugin.logic.querychannel
3838import io.getstream.chat.android.client.internal.state.plugin.state.global.internal.MutableGlobalState
3939import io.getstream.chat.android.client.parser2.SyncedEvents
4040import io.getstream.chat.android.client.persistance.repository.RepositoryFacade
41+ import io.getstream.chat.android.client.persistance.repository.write
4142import io.getstream.chat.android.client.query.CreateChannelParams
4243import io.getstream.chat.android.client.setup.state.ClientState
4344import io.getstream.chat.android.client.sync.SyncState
@@ -619,16 +620,18 @@ internal class SyncManager(
619620 return
620621 }
621622 logger.d { " [updateAllReadStateForDate] userId: $userId , currentDate: $currentDate " }
622- val syncState = repos.selectSyncState(userId)?.let { selectedState ->
623- logger.v {
624- " [updateAllReadStateForDate] selectedState.activeCids.zie: ${selectedState.activeChannelIds.size} "
625- }
626- when (selectedState.markedAllReadAt?.before(currentDate)) {
627- true -> selectedState.copy(markedAllReadAt = currentDate).also { newState ->
628- repos.insertSyncState(newState)
623+ val syncState = repos.write {
624+ repos.selectSyncState(userId)?.let { selectedState ->
625+ logger.v {
626+ " [updateAllReadStateForDate] selectedState.activeCids.zie: ${selectedState.activeChannelIds.size} "
629627 }
628+ when (selectedState.markedAllReadAt?.before(currentDate)) {
629+ true -> selectedState.copy(markedAllReadAt = currentDate).also { newState ->
630+ repos.insertSyncState(newState)
631+ }
630632
631- else -> selectedState
633+ else -> selectedState
634+ }
632635 }
633636 } ? : SyncState (userId)
634637 setSyncState(syncState)
@@ -660,26 +663,32 @@ internal class SyncManager(
660663 rescueStuckInProgress(" channels" ) {
661664 val cids = repos.selectChannelCidsBySyncStatus(SyncStatus .IN_PROGRESS )
662665 logger.d { " [rescueEntitiesStuckInProgress] cids.size: ${cids.size} " }
666+ // A write for each, so one that fails keeps the ones before it handed back.
663667 cids.forEach { cid ->
664- val channel = repos.selectChannel(cid) ? : return @forEach
665- repos.insertChannel(channel.copy(syncStatus = SyncStatus .SYNC_NEEDED ))
668+ repos.write {
669+ repos.selectChannel(cid)?.let { repos.insertChannel(it.copy(syncStatus = SyncStatus .SYNC_NEEDED )) }
670+ }
666671 }
667672 }
668673 rescueStuckInProgress(" messages" ) {
669- // An old delete left in progress almost always reached the backend and lost only its result, and the
670- // retry would just remove it locally. A delete for the current user has no local date to tell its age.
671- repos.markDeletesInProgressAsCompleted(deletedBefore = Date (now() - syncMaxThreshold.millis))
672- val messageIds = repos.selectMessageIdsBySyncState(SyncStatus .IN_PROGRESS )
673- logger.d { " [rescueEntitiesStuckInProgress] messageIds.size: ${messageIds.size} " }
674- // Status only, since every insert of a deleted message is dropped
675- messageIds.forEach { id -> repos.updateMessageSyncStatus(id, SyncStatus .SYNC_NEEDED ) }
674+ repos.write {
675+ // An old delete left in progress almost always reached the backend and lost only its result, and the
676+ // retry would just remove it locally. A delete for the current user has no local date to tell its age.
677+ repos.markDeletesInProgressAsCompleted(deletedBefore = Date (now() - syncMaxThreshold.millis))
678+ val messageIds = repos.selectMessageIdsBySyncState(SyncStatus .IN_PROGRESS )
679+ logger.d { " [rescueEntitiesStuckInProgress] messageIds.size: ${messageIds.size} " }
680+ // Status only, since every insert of a deleted message is dropped
681+ messageIds.forEach { id -> repos.updateMessageSyncStatus(id, SyncStatus .SYNC_NEEDED ) }
682+ }
676683 }
677684 rescueStuckInProgress(" reactions" ) {
678685 val reactionIds = repos.selectReactionIdsBySyncStatus(SyncStatus .IN_PROGRESS )
679686 logger.d { " [rescueEntitiesStuckInProgress] reactionIds.size: ${reactionIds.size} " }
680687 reactionIds.forEach { id ->
681- val reaction = repos.selectReactionById(id) ? : return @forEach
682- repos.insertReaction(reaction.copy(syncStatus = SyncStatus .SYNC_NEEDED ))
688+ repos.write {
689+ repos.selectReactionById(id)
690+ ?.let { repos.insertReaction(it.copy(syncStatus = SyncStatus .SYNC_NEEDED )) }
691+ }
683692 }
684693 }
685694 }
@@ -997,7 +1006,7 @@ internal class SyncManager(
9971006 ?.updateDataForChannel(channel, channel.messages.size, fromServer = true )
9981007 }
9991008 mutableGlobalState.updateChannelDrafts(foundChannels)
1000- repos.storeStateForChannels(foundChannels)
1009+ repos.write { repos. storeStateForChannels(foundChannels) }
10011010 val foundCids = foundChannels.mapTo(mutableSetOf (), Channel ::cid)
10021011 val stillMissingChannelIds = requestedIds.filterNot { it.cid in foundCids }
10031012 logger.v { " [updateActiveChannels] stillMissingCids.size: ${stillMissingChannelIds.size} " }
@@ -1081,7 +1090,7 @@ internal class SyncManager(
10811090 ?.updateDataForChannel(channel, channel.messages.size, fromServer = true )
10821091 }
10831092 mutableGlobalState.updateChannelDrafts(foundChannels)
1084- repos.storeStateForChannels(foundChannels)
1093+ repos.write { repos. storeStateForChannels(foundChannels) }
10851094 foundChannels.mapTo(refreshedCids, Channel ::cid)
10861095 }
10871096 // Stop at the first failure: the remaining requests would be spent on a refresh the
@@ -1118,7 +1127,7 @@ internal class SyncManager(
11181127 val channel = repos.selectChannel(cid) ? : return @forEach
11191128 val result = if (channel.createdAt.exceedsSyncThreshold()) {
11201129 logger.w { " [retryChannels] outdated channel($cid )" }
1121- repos.deleteChannel(cid)
1130+ repos.write { repos. deleteChannel(cid) }
11221131 uncreatedCids + = cid
11231132 discardedCids + = cid
11241133 Result .Success (Unit )
@@ -1139,8 +1148,10 @@ internal class SyncManager(
11391148 if (retriedCid != cid) {
11401149 // The create stores and replaces the placeholder regenerated from the members, so one stored with
11411150 // another member order is renamed first, or it would be left behind next to the regenerated one
1142- repos.insertChannel(channel.copy(id = retriedCid.substringAfter(' :' )))
1143- repos.replacePlaceholderChannel(cid, retriedCid)
1151+ repos.write {
1152+ repos.insertChannel(channel.copy(id = retriedCid.substringAfter(' :' )))
1153+ repos.replacePlaceholderChannel(cid, retriedCid)
1154+ }
11441155 }
11451156 // The server rejects the placeholder id, so it resolves the distinct channel from the members
11461157 val channelId = if (isPlaceholder) " " else channel.id
@@ -1308,7 +1319,7 @@ internal class SyncManager(
13081319
13091320 private suspend fun markMessageAsFailed (message : Message ): Result <Unit > = try {
13101321 val failed = message.copy(syncStatus = SyncStatus .FAILED_PERMANENTLY , updatedLocallyAt = Date (now()))
1311- repos.insertMessage(failed)
1322+ repos.write { repos. insertMessage(failed) }
13121323 logicRegistry.channelFromMessage(failed)?.upsertMessage(failed)
13131324 logicRegistry.getActiveQueryThreadsLogic().forEach { it.upsertMessage(failed) }
13141325 logicRegistry.threadFromMessage(failed)?.upsertMessage(failed)
@@ -1398,12 +1409,16 @@ internal class SyncManager(
13981409 try {
13991410 logger.d { " [removeReaction] reaction.id: ${reaction.id} " }
14001411 // A dropped deletion leaves its message alone: the reaction is still on the server.
1401- if (reaction.deletedAt == null ) {
1402- removeReactionFromState(reaction)
1403- repos.selectMessage(reaction.messageId)?.let { repos.insertMessage(it.removeMyReaction(reaction)) }
1412+ val onMessage = reaction.deletedAt == null
1413+ if (onMessage) removeReactionFromState(reaction)
1414+ // One write, so a failure leaves the row for the next sync to drop again.
1415+ repos.write {
1416+ if (onMessage) {
1417+ repos.selectMessage(reaction.messageId)
1418+ ?.let { repos.insertMessage(it.removeMyReaction(reaction)) }
1419+ }
1420+ repos.deleteReaction(reaction)
14041421 }
1405- // Deleted last, so a failure above leaves the row for the next sync to drop again.
1406- repos.deleteReaction(reaction)
14071422 logger.v { " [removeReaction] completed: ${reaction.id} " }
14081423 Result .Success (Unit )
14091424 } catch (e: Throwable ) {
@@ -1430,7 +1445,7 @@ internal class SyncManager(
14301445 return CoroutineCall (syncScope) {
14311446 try {
14321447 logger.d { " [removeMessage] message.id: ${message.id} " }
1433- repos.deleteChannelMessage(message)
1448+ repos.write { repos. deleteChannelMessage(message) }
14341449 logicRegistry.channelFromMessage(message)?.deleteMessage(message)
14351450 logicRegistry.getActiveQueryThreadsLogic().forEach { it.deleteMessage(message) }
14361451 logicRegistry.threadFromMessage(message)?.deleteMessage(message)
0 commit comments