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

pull/37/head
Vitor Pamplona 2023-01-23 20:54:56 -03:00
rodzic b6c25cfa46
commit e064741148
7 zmienionych plików z 43 dodań i 32 usunięć

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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