Parallelize feed properly

This commit is contained in:
Jobobby04 2023-02-07 14:03:27 -05:00
parent f93cf29df4
commit a19be83f99
2 changed files with 64 additions and 56 deletions

View File

@ -32,6 +32,8 @@ import eu.kanade.tachiyomi.util.system.logcat
import exh.savedsearches.models.FeedSavedSearch
import exh.savedsearches.models.SavedSearch
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
@ -227,7 +229,8 @@ open class FeedScreenModel(
*/
private fun getFeed(feedSavedSearch: List<FeedItemUI>) {
coroutineScope.launch {
feedSavedSearch.forEach { itemUI ->
feedSavedSearch.map { itemUI ->
async {
val page = try {
if (itemUI.source != null) {
withContext(coroutineDispatcher) {
@ -248,13 +251,13 @@ open class FeedScreenModel(
emptyList()
}
val result = itemUI.copy(
val result = withIOContext {
itemUI.copy(
results = page.map {
withIOContext {
networkToLocalManga.await(it.toDomainManga(itemUI.source!!.id))
}
},
)
}
mutableState.update { state ->
state.copy(
@ -262,6 +265,7 @@ open class FeedScreenModel(
)
}
}
}.awaitAll()
}
}

View File

@ -31,6 +31,8 @@ import eu.kanade.tachiyomi.util.system.logcat
import exh.savedsearches.models.FeedSavedSearch
import exh.savedsearches.models.SavedSearch
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@ -127,7 +129,8 @@ open class SourceFeedScreenModel(
*/
private fun getFeed(feedSavedSearch: List<SourceFeedUI>) {
coroutineScope.launch {
feedSavedSearch.forEach { sourceFeed ->
feedSavedSearch.map { sourceFeed ->
async {
val page = try {
withContext(coroutineDispatcher) {
when (sourceFeed) {
@ -144,8 +147,8 @@ open class SourceFeedScreenModel(
emptyList()
}
val titles = page.map {
withIOContext {
val titles = withIOContext {
page.map {
networkToLocalManga.await(it.toDomainManga(source.id))
}
}
@ -156,6 +159,7 @@ open class SourceFeedScreenModel(
)
}
}
}.awaitAll()
}
}