Batch sync news resources from remote

Batch sync news resources from remote to reduce json payload size.
pull/608/head
Adetunji Dahunsi 2 years ago committed by GitHub
parent 0844d021b9
commit 267adfd27f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -34,6 +34,8 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import javax.inject.Inject import javax.inject.Inject
private const val SYNC_BATCH_SIZE = 40
/** /**
* Disk storage backed implementation of the [NewsRepository]. * Disk storage backed implementation of the [NewsRepository].
* Reads are exclusively from local storage to support offline access. * Reads are exclusively from local storage to support offline access.
@ -65,26 +67,29 @@ class OfflineFirstNewsRepository @Inject constructor(
}, },
modelDeleter = newsResourceDao::deleteNewsResources, modelDeleter = newsResourceDao::deleteNewsResources,
modelUpdater = { changedIds -> modelUpdater = { changedIds ->
val networkNewsResources = network.getNewsResources(ids = changedIds) changedIds.chunked(SYNC_BATCH_SIZE).forEach { chunkedIds
val networkNewsResources = network.getNewsResources(ids = chunkedIds)
// Order of invocation matters to satisfy id and foreign key constraints! // Order of invocation matters to satisfy id and foreign key constraints!
topicDao.insertOrIgnoreTopics( topicDao.insertOrIgnoreTopics(
topicEntities = networkNewsResources topicEntities = networkNewsResources
.map(NetworkNewsResource::topicEntityShells) .map(NetworkNewsResource::topicEntityShells)
.flatten() .flatten()
.distinctBy(TopicEntity::id), .distinctBy(TopicEntity::id),
) )
newsResourceDao.upsertNewsResources( newsResourceDao.upsertNewsResources(
newsResourceEntities = networkNewsResources newsResourceEntities = networkNewsResources.map(
.map(NetworkNewsResource::asEntity), NetworkNewsResource::asEntity
) ),
newsResourceDao.insertOrIgnoreTopicCrossRefEntities( )
newsResourceTopicCrossReferences = networkNewsResources newsResourceDao.insertOrIgnoreTopicCrossRefEntities(
.map(NetworkNewsResource::topicCrossReferences) newsResourceTopicCrossReferences = networkNewsResources
.distinct() .map(NetworkNewsResource::topicCrossReferences)
.flatten(), .distinct()
) .flatten(),
)
}
}, },
) )
} }

Loading…
Cancel
Save