I build an app that is able to deploy a periodic work using workmanager and I want to perform some actions like saving key-value in sharePreference or uploading data to firebase when the work is told to be stopped either by users themselves or somehow the system, through overriding the onStopped method. But it doesn't work. I also tested it by setting a button to manually cancel the work using cancelAllWorkByTag to check how it works and nothing is working. Can someone help me out?
override fun onStopped() {
super.onStopped()
val sharedPreferences = applicationContext.getSharedPreferences("AppSharedPreference", Context.MODE_PRIVATE)
with(sharedPreferences.edit()) {
putString("working_state", "STOPPED")
commit()
}
val db = Firebase.firestore
val now = DateTime.now()
val input = hashMapOf("timestamp" to now.toString(), "stopped" to "the service is somehow stopped")
db.collection("test").document(now.toString()).set(input, SetOptions.merge())
}
Related
I have this composable that represents an "edit data" screen:
#Composable
fun EditNodeScreen(
vm: EditNodeViewModel,
canceled: () -> Unit,
accepted: (id: UUID) -> Unit
) {
// ...
Button(onClick = {
val id = vm.save()
accepted(id)
}) {
Text(text = "Save")
}
}
Except, EditNodeViewModel.save() is actually a suspend function, so I can't just call it like that.
What I can find says that I should create a coroutine scope with rememberCoroutineScope(), then use that to launch a coroutine:
onClick = {
coroutineScope.launch {
val id = vm.save()
accepted(id) // side question: do I have to switch back to Main context?
}
}
But the documentation also says that this coroutine will be canceled if the composition is detached. I do not want to cancel the save process once it is commenced!
Is this still the right thing to do, or is there a better way? Should I use GlobalScope.launch perhaps?
If you have to handle an operation that should be completed even if the user navigates away from the screen, use WorkManager.
From the Docs,
WorkManager is intended for work that is required to run reliably even
if the user navigates off a screen, the app exits, or the device
restarts.
Use Expedited work to start the task immediately.
1.
I am using:
override fun updateNotification(mediaSession: MediaSessionCompat) {
if (!PlayerService.IS_RUNNING) return
GlobalScope.launch {
notificationManager.notify(NOTIFICATION_ID, buildNotification(mediaSession))
}
}
I could use:
override fun updateNotification(mediaSession: MediaSessionCompat) {
if (!BeatPlayerService.IS_RUNNING) return
CoroutineScope(Dispatchers.IO).launch {
notificationManager.notify(NOTIFICATION_ID, buildNotification(mediaSession))
}
}
2.
I am using:
GlobalScope.launch {
while (true) {
delay(100)
mediaMediaConnection.mediaController ?: continue
val newTime = mediaMediaConnection.mediaController?.playbackState?.position
if (state == BIND_STATE_BOUND) newTime?.toInt()?.let { update(it) }
if (state == BIND_STATE_CANCELED) break
}
}
I could use:
CoroutineScope(Dispatchers.IO).launch {
while (true) {
delay(100)
mediaMediaConnection.mediaController ?: continue
val newTime = mediaMediaConnection.mediaController?.playbackState?.position
if (state == BIND_STATE_BOUND) newTime?.toInt()?.let { update(it) }
if (state == BIND_STATE_CANCELED) break
}
}
I dont see any visible difference while using GlobalScope.launch versus CoroutineScope().launch in my music app.
Can someone explain which is better to use in my context of 1 and 2
I have seen:
Why not use GlobalScope.launch?
but don't quite understand fully especially in my use case.
Can someone explain which is better to use in my context of 1 and 2
Neither.
The problem with both of them is that they are pretty much unconstrained. They don't respect app's lifecycle which means that if your app gets closed (or user goes away from that screen) while some coroutines were running inside those scopes, they won't stop executing and will leak the coroutines(the app might even crash)
When can my app crash:
GlobalScope.launch {
fetchUserData() // --> this is a suspend function
updateUI() // e.g. progressBar.isVisible = false
}
Here if user navigates away from the screen while fetchUserData was suspended. Then that code won't stop executing and when fetchUserData returns, it will try to update the UI but since the screen has changed, it will throw an exception and your app will crash.
What we want is that coroutines should stop when they are no longer needed. If you use GlobalScope you loose all that control. For the case of a custom CoroutineScope, if you are cancelling it at the right time everything is good otherwise it's also harmful.
What's the alternative?
You can/should use the built-in scopes that android provides like lifecycleScope and viewModelScope. The first one follows the lifecycle of the activity (or fragment) while the second one gets cleaned up when view model is destoryed.
So, that was the general advice. Coming to your particular case,
In your first case, you don't even need a coroutine scope because notify is a very simple function (neither suspend nor blocking)
In the second case, you have an infinite loop that breaks when a particular condition is met. Since you have this code inside a ViewModel, you most likely don't want it to run after the view model gets cleared. If you use CoroutineScope() or GlobalScope you won't be able to control that. So you should use a viewModelScope here so that the infinite loop stops when the work is no longer required.
I'm using Android Kotlin with the SDK 30 and Coroutine 1.4.1.
I have a function that handles incoming messages to display them on my app in a form of temperature measurement. I use CoroutineScope to process the data and save it in the database. These messages are received from a socket.io connection. The problem is that the messages are not displayed in the correct order when a bulk of data is flowing in.
Now I've looked at my nodejs logs and these messages are sent in the correct order so it can't be that.
I'm using a standard Coroutine function.
See below.
fun receiveTmps(data){
CoroutineScope(IO).launch {
val usersJob = launch {
usersBg(data)
}
}
}
Now I know that with Coroutine I can add a join to wait for the job to finish before starting the next one. But because the messages do not come in at once, but flow continuously over a period of 5 to 20 seconds, it is possible that one message is completed faster than the older one. This causes incorrect order.
My question is, is there any way to handle these tasks 1 by 1 while adding multiple jobs to the list?
Any suggestion or idea is appreciated.
Thank you in advance.
UPDATED:
From what I read from the documentation you should also cancel the channel after its done. So that's going to be tricky to close the channel when messages are flowing in and since I don't have a clear number of what's flowing in I'm having a hard time defining that to the channel. I have tested several ways but most of the examples doesnt work or are outdated.
This is the most basic working example but it always has a defined repeat.
val channel = Channel<String>(UNLIMITED)
fun receiveTmps(data:String){
CoroutineScope(Dispatchers.Default).launch {
channel.send(data)
}
}
#ExperimentalCoroutinesApi
fun main() = runBlocking<Unit> {
launch {
// while(!channel.isClosedForReceive){
// val x = channel.receive()
// Log.d("deb", "Temperature.. "+ x)
// }
repeat(3) {
val x = channel.receive()
Log.d("deb", "Temperature.. "+ x)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
receiveTmps("10")
receiveTmps("30")
// Many more...
main()
}
If we need to process some events sequentially then typical solution is to create a queue of events and start a single consumer to process them. In the case of coroutines we can use Channel as a queue and launch a coroutine running in a loop that will be our consumer.
I'm not very familiar with Android, so I may miss something, but it should be something along lines:
fun receiveTmps(data:String){
channel.trySend(data).getOrThrow()
}
fun main() {
lifecycleScope.launch(Dispatchers.Default) {
for (tmp in channel) {
...
}
}
}
My assumption is that you want to stop processing events when the activity/service will be destroyed, ignoring all temps that are still waiting in the queue.
I'm working on an Android app with a constant repeating background process.
From the moment the device starts it should load data off a webpage every minute. It uses XmlPullParser and a simple URL inputstream. It is but 10kb so it isn't that intensive. I believe this kind of task is called Deferred. The information loaded by the process has to be accessible to the Activity once that the user opens the app. The background process also needs to be abled to place a notification once the data shows certain results.
There seem to be multiple methods to achieve this in Android, eg. a JobScheduler, WorkManager or AlarmManager however everything I've tried so far seems to either stop once the activity closes or doesn't run at all. The timing, every minute, also seems to be an issue as for both a repeating job and worker the minimum interval is 15. This one minute doesn't have to be exact. I imagine instead of having a repeating process loading the data once it might be better to have a long running process sleeping for 1m in between loading the data.
I do not have access to the server the application is connecting to. so I can't do a FirebaseMessagingService.
What would be the best way to schedule such a background process?
How can the activity best exchange information with that process?
I'm open for all suggestions,
thank you for your time.
Easy with WorkManager, it's the most encouraged way for Scheduling Repeating background work in Android, see introduction.
As you say, the minimum repeating work request interval is restricted to 15 minutes, the only way to break it is to Repeatedly schedule the one-time work.
1. Setup Your Worker Class:
class ToastShower(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
withContext(Dispatchers.Main) { //ui related work must run in Main thread!!
Toast.makeText(applicationContext, "Hey, I'm Sam! This message will appear every 5 seconds.", Toast.LENGTH_SHORT).show()
}
return Result.success()
}
}
2. Setup Your Custom Application Class:
class WorkManagerApplication : Application() {
private val backgroundScope = CoroutineScope(Dispatchers.Default) //standard background thread
private val applicationContext = this
override fun onCreate() { //called when the app launches (same as Activity)
super.onCreate()
initWork()
}
private fun initWork() {
backgroundScope.launch { //all rnu in background thread
setupToastShowingWork(0) //no delay at first time
observeToastShowingWork() //observe work state changes, see below
}
}
private fun setupToastShowingWork(delayInSeconds: Long) { //must run in background thread
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED) //when using WiFi
.build()
val oneTimeRequest = OneTimeWorkRequestBuilder<ToastShower>() //【for breaking 15 minutes limit we have to use one time request】
.setInitialDelay(delayInSeconds, TimeUnit.SECONDS) //customizable delay (interval) time
.setConstraints(constraints)
.build()
WorkManager.getInstance(applicationContext).enqueueUniqueWork( //【must be unique!!】
ToastShower::class.java.simpleName, //work name, use class name for convenient
ExistingWorkPolicy.KEEP, //if new work comes in with same name, discard the new one
oneTimeRequest
)
}
private suspend fun observeToastShowingWork() {
withContext(Dispatchers.Main) { //must run in Main thread for using observeForever
WorkManager.getInstance(applicationContext).getWorkInfosForUniqueWorkLiveData(ToastShower::class.java.simpleName).observeForever {
if (it[0].state == WorkInfo.State.SUCCEEDED) { //when the work is done
backgroundScope.launch { //prevent from running in Main thread
setupToastShowingWork(5) //every 5 seconds
}
}
}
}
}
}
3. Setup AndroidManifest File:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.workmanagertest">
<application
android:name=".WorkManagerApplication" //【here, must!!!】
...
</application>
</manifest>
By setting up with above, the work (showing Toast in my example) will be executed (or more clearly, schedule and execute) every 5 seconds no matter the app is in foreground or background or killed by system. Only way to stop it is either uninstall or go inside the app's setting to force-close it.
Demo: https://youtu.be/7IsQQppKqFs
As explained in some answers:
On Android, Firebase automatically manages connection state to reduce bandwidth and battery usage. When a client has no active listeners, no pending write or onDisconnect operations, and is not explicitly disconnected by the goOffline method, Firebase closes the connection after 60 seconds of inactivity.
The problem is that after 60s, even after I go to an activity with a complete new reference, event listener, etc.. It still says it is disconnect, when in fact, it is not.
val connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected")
var connectListener : ValueEventListener? = null
fun checkConnection() {
connectListener = connectedRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val connected = snapshot.getValue(Boolean::class.java)!!
if (connected) {
Log.d("FRAG", "CONNECTED")
else{
Log.d("FRAG", "DISCONNECTED")
}
}
override
fun onCancelled(error: DatabaseError) {
System.err.println("Listener was cancelled")
}
})
}
override fun onDetach() {
super.onDetach()
if (connectListener != null){
connectedRef.removeEventListener(connectListener)
}
}
How can I make sure I maintain or create a new connection to Firebase? I call the checkConnection method every onAttach of a fragment and onStart of an activity.
If you have an active listener on any data that is read from the server, the connection should remain open unless you've explicitly called goOffline() in your code. Note that .info/connected itself does not require reading from the server, so does not keep the connection open.
It seems you're using the realtime database to build an presence system on an otherwise Firestore based app. In that case: Cloud Firestore uses a gRPC-based protocol to talk between client and server, while the Firebase Realtime Database uses web sockets. They're in no way compatible or even comparable. Keeping an active listener on data in Firestore does not keep a connection to RTDB open. That's why the example in the Firestore documentation also writes an actual data node to the realtime database.
Stream<Event> checkInternetConectivity() {
Stream<Event> connectivityCheck = _firebaseDatabase.reference().child('.info/connected').onValue;
Stream<Event> randomCheck = _firebaseDatabase.reference().child('connected').onValue;
return Rx.combineLatest2(connectivityCheck, randomCheck,(connectivityCheck, _) => connectivityCheck as Event);}
}
Firebase automatically disconnects from the realtime database in android after 60 seconds if there are no active listeners and listening to '.info/connected' isn't enough to keep the connection active. Creating another stream to listen to a random node in realtime database as a way around to this automatic disconnection.
This is my workaround to this problem in Dart/Flutter