问题
I'm trying to wrap my head around the behaviour of setTag and setUpdateCurrent of OneoffTask in the GcmNetworkManager tooling.
The documentation of setUpdateCurrent says this:
Optional setter to specify whether this task should override any preexisting tasks with the same tag. This defaults to false, which means that a new task will not override an existing one.
Fair enough, but it doesn't say what will happen to a task, it only says one thing that will not happen - i.e. the task will not overridden. :) It's not clear to me if using setUpdateCurrent means that duplicate tasks are allowed of it means that they are discarded?
To be clear, my question is this: Given that you used setUpdateCurrent(false), when you create a OneoffTask with the same tag as another OneoffTask that is already scheduled, will the new task still happen in addition to the already scheduled one, or will the new task be thrown away, dismissed as a duplicate?
回答1:
Given that you used setUpdateCurrent(false), when you create a OneoffTask with the same tag as another OneoffTask that is already scheduled, will the new task still happen in addition to the already scheduled one, or will the new task be thrown away, dismissed as a duplicate?
The new task is thrown away and is never scheduled. I was surprised by this and definitely feel the documentation should have stated so.
Turns out you can test this yourself using the following command:
adb shell dumpsys activity service GcmService --endpoints YourGcmTaskServiceClassName
This allows you to see how many tasks you have pending among other things. Immediately after the call to GcmNetworkManager.getInstance(this).schedule(task)
in your code the number of tasks for you package will be incremented in the output of the adb command:
Tasks count by package:
com.mypackage: 1
and beneath that is an area that shows the pending tasks:
Pending:
(scheduled) endpoint='com.mypackage/com.mypackage.YourGcmTaskServiceClassName'
tag='testing' : [PENDING] u0
Next execution: [early=17s, expires=37s]
Not yet run.
Now if you try and schedule another task with setUpdateCurrent(false)
and the same tag as the task that is currently pending you will notice the next time you run the adb command only one task is still pending and it is the first task you scheduled.
If you specify setUpdateCurrent(true)
one task also remains scheduled but as expected it will be the new task.
Finally, if you specify tasks with different tags and schedule them the task count will become two and both tasks will be pending which is also expected.
来源:https://stackoverflow.com/questions/34528960/clarification-of-setupdatecurrent-on-oneofftask