I'm currently using Firebase JobDispatcher to run a periodic check in the background using the service intent (registered on boot, install & update). This is working correctly, and it spawns it's own thread separate from the main thread, so I don't lock the UI, and it kills the thread properly so garbage collection doesn't become a problem.
I would also like to trigger this same exact work from the main thread in-app. The problem is that running a jobdispatcher onCreate triggers the job to run in the main UI thread, and not in a background thread. One of my functions can generate A LOT of objects, so even though it properly runs, it kills usability in the app because the GC never can get rid of all the junk.
How can I run an existing JobDispatcher job on demand from the main thread, but still in a separate thread?
Thanks so much for your help.
Edits for clarification of this specific usage
My particular case involves a first run, where UpdateNetworkerJob.class can take several minutes to run. The phoneHistoryCheck.GetLastCallChecked() function has a loop which makes several calls to Firebase, both retrieving and putting information.
9/12 Update
Subsequent runs are very quick, and AsyncTask can work. However I am finding I need a more robust solution and am currently looking at ThreadPoolExecutor. It's possible that the GetLastCallChecked() function is throwing too much at the Firebase Engine and it causing the main thread's calls to Firebase to be way down in the noise as it catches up. I'm working on sorting this part out.
Code in main thread to start the work:
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(trackedContactListActivity.this));
Job myJob = dispatcher.newJobBuilder()
.setService(UpdateNetworkerJob.class)
.setTag("UpdateNetworkerService")
.setTrigger(Trigger.NOW)
.setConstraints(
Constraint.ON_ANY_NETWORK
)
.build();
dispatcher.mustSchedule(myJob);
UpdateNetworkerJob.class
public class UpdateNetworkerJob extends JobService {
private static final String TAG = "UpdateNetworkerJob";
@Override
public boolean onStartJob(final JobParameters params) {
Log.d(TAG, "UpdateNetworkerJob is running with params: " + params);
PhoneHistoryCheck phoneHistoryCheck = new PhoneHistoryCheck();
phoneHistoryCheck.GetLastCallChecked(UpdateNetworkerJob.this);
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
// Stop tracking these job parameters, as we've 'finished' executing.
//sendMessage(MSG_COLOR_STOP, params.getJobId());
Log.d(TAG, "on stop job: " + params);
// Return false to drop the job.
return false;
}
}