Skip to content

Commit

Permalink
logSyncInSharedPrefs improvements to prevent ANR
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Dec 11, 2024
1 parent 69bc403 commit b2e518d
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import androidx.fragment.app.FragmentManager
import androidx.lifecycle.lifecycleScope
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.google.android.material.bottomnavigation.BottomNavigationView
import io.realm.Realm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.callback.OnRatingChangeListener
import org.ole.planet.myplanet.model.RealmUserChallengeActions.Companion.createAction
Expand Down Expand Up @@ -121,10 +124,24 @@ abstract class DashboardElementActivity : SyncActivity(), FragmentManager.OnBack
}

fun logSyncInSharedPrefs() {
lifecycleScope.launch {
if (isServerReachable(Utilities.getUrl())) {
startUpload("dashboard")
createAction(mRealm, "${profileDbHandler.userModel?.id}", null, "sync")
lifecycleScope.launch(Dispatchers.IO + SupervisorJob()) {
try {
val isReachable = isServerReachable(Utilities.getUrl())
if (isReachable) {
withContext(Dispatchers.Main) {
startUpload("dashboard")
}

withContext(Dispatchers.IO) {
Realm.getDefaultInstance().use { realm ->
realm.executeTransaction {
createAction(realm, "${profileDbHandler.userModel?.id}", null, "sync")
}
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Expand Down

0 comments on commit b2e518d

Please sign in to comment.