Moving coroutines from Main to Default, which is more ideal for memory intensive tasks

This commit is contained in:
Vitor Pamplona 2023-01-23 20:54:56 -03:00
parent b6c25cfa46
commit e064741148
7 changed files with 43 additions and 32 deletions

View File

@ -110,7 +110,7 @@ class Note(val idHex: String) {
if (handlerWaiting) return if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
delay(100) delay(100)
live.refresh() live.refresh()

View File

@ -182,7 +182,7 @@ class User(val pubkey: ByteArray) {
if (handlerWaiting) return if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
delay(100) delay(100)
live.refresh() live.refresh()

View File

@ -1,7 +1,11 @@
package com.vitorpamplona.amethyst.service.relays package com.vitorpamplona.amethyst.service.relays
import android.util.Log
import com.google.gson.JsonElement import com.google.gson.JsonElement
import com.vitorpamplona.amethyst.model.LocalCache
import nostr.postr.events.ContactListEvent
import nostr.postr.events.Event import nostr.postr.events.Event
import nostr.postr.toNpub
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
@ -34,7 +38,6 @@ class Relay(
} }
fun requestAndWatch() { fun requestAndWatch() {
println("Connecting with ${url}")
val request = Request.Builder().url(url).build() val request = Request.Builder().url(url).build()
val listener = object : WebSocketListener() { val listener = object : WebSocketListener() {
@ -98,8 +101,8 @@ class Relay(
socket?.close(1000, "Normal close") socket?.close(1000, "Normal close")
// Failures disconnect the relay. // Failures disconnect the relay.
socket = null socket = null
//println("Relay onFailure ${url}, ${response?.message}") Log.w("Relay", "Relay onFailure ${url}, ${response?.message}")
t.printStackTrace() //t.printStackTrace()
listeners.forEach { listeners.forEach {
it.onError(this@Relay, "", Error("WebSocket Failure. Response: ${response}. Exception: ${t.message}", t)) it.onError(this@Relay, "", Error("WebSocket Failure. Response: ${response}. Exception: ${t.message}", t))
} }

View File

@ -27,7 +27,7 @@ class CardFeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
private var lastNotes: List<Note>? = null private var lastNotes: List<Note>? = null
fun refresh() { fun refresh() {
val scope = CoroutineScope(Job() + Dispatchers.IO) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
refreshSuspended() refreshSuspended()
} }
@ -92,16 +92,17 @@ class CardFeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
} }
var handlerWaiting = false var handlerWaiting = false
@Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting) return synchronized(handlerWaiting) {
if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.IO) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
delay(100) delay(100)
refresh() refresh()
handlerWaiting = false handlerWaiting = false
}
} }
} }

View File

@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.ui.screen package com.vitorpamplona.amethyst.ui.screen
import android.util.Log
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
@ -63,7 +64,7 @@ abstract class FeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel()
} }
fun refresh() { fun refresh() {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.Default) {
val notes = newListFromDataSource() val notes = newListFromDataSource()
val oldNotesState = feedContent.value val oldNotesState = feedContent.value
@ -90,16 +91,17 @@ abstract class FeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel()
} }
var handlerWaiting = false var handlerWaiting = false
@Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting) return synchronized(handlerWaiting) {
if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.IO) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
delay(100) delay(100)
refresh() refresh()
handlerWaiting = false handlerWaiting = false
}
} }
} }

View File

@ -33,7 +33,7 @@ open class UserFeedViewModel(val dataSource: NostrDataSource<User>): ViewModel()
val feedContent = _feedContent.asStateFlow() val feedContent = _feedContent.asStateFlow()
fun refresh() { fun refresh() {
val scope = CoroutineScope(Job() + Dispatchers.IO) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
refreshSuspended() refreshSuspended()
} }
@ -64,16 +64,17 @@ open class UserFeedViewModel(val dataSource: NostrDataSource<User>): ViewModel()
} }
var handlerWaiting = false var handlerWaiting = false
@Synchronized
fun invalidateData() { fun invalidateData() {
if (handlerWaiting) return synchronized(handlerWaiting) {
if (handlerWaiting) return
handlerWaiting = true handlerWaiting = true
val scope = CoroutineScope(Job() + Dispatchers.IO) val scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch { scope.launch {
delay(100) delay(100)
refresh() refresh()
handlerWaiting = false handlerWaiting = false
}
} }
} }

View File

@ -18,6 +18,10 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
fun NotificationScreen(accountViewModel: AccountViewModel, navController: NavController) { fun NotificationScreen(accountViewModel: AccountViewModel, navController: NavController) {
val feedViewModel: CardFeedViewModel = viewModel { CardFeedViewModel( NostrNotificationDataSource ) } val feedViewModel: CardFeedViewModel = viewModel { CardFeedViewModel( NostrNotificationDataSource ) }
LaunchedEffect(Unit) {
feedViewModel.refresh()
}
Column(Modifier.fillMaxHeight()) { Column(Modifier.fillMaxHeight()) {
Column( Column(
modifier = Modifier.padding(vertical = 0.dp) modifier = Modifier.padding(vertical = 0.dp)