levibostian / Wendy-Android

Build offline first Android mobile apps. Remove loading screens, perform tasks instantly.
https://levibostian.github.io/Wendy-Android/wendy/
MIT License
64 stars 16 forks source link

Wendy not syncing periodically #60

Closed essieM closed 4 years ago

essieM commented 4 years ago

So I've just configured Wendy on my app and have followed the sample code provided. I'm however noticing that, after the sync is done the first time, it doesn't happen again. I'm calling the sync method the first time a user logs in.

I'm still learning about the library, but my assumption was that as long as I've configured and initialized it in my application class, then configured the PendingTasksJob class as well, Wendy would be running every 15 min or whatever period of time this is set to.

My PendingTasksJob class looks as follows, just like in the sample code provided:

`internal class PendingTasksJob : Job() {

override fun onRunJob(params: Params): Job.Result {
    runTheJob()

    return Job.Result.SUCCESS
}

private fun runTheJob() {
    if (WendyConfig.automaticallyRunTasks) {
        LogUtil.d("Wendy configured to automatically run tasks. Running the periodically scheduled job.")
        Wendy.sharedInstance().runTasks(null)
    } else LogUtil.d("Wendy configured to *not* automatically run tasks. Skipping execution of periodically scheduled job.")
}

companion object {
    const val TAG = "WendyPendingTasksJob"

    fun scheduleJob() {
        JobRequest.Builder(PendingTasksJob.TAG)
                .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
                .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(5))
                .setRequirementsEnforced(true)
                .setUpdateCurrent(true)
                .build()
                .schedule()
    }
}

}`

Am I missing something else? I would truly appreciate some help with this.

Thanks.

levibostian commented 4 years ago

Hey there, @essieM. Welcome to Wendy!

I am sorry you have been having this issue. The problem is that the documentation is not complete. I did not realize this until now!

Wendy does not run periodically on it's own. You need to use something like workmanager to run all Wendy tasks periodically.

essieM commented 4 years ago

Will you be adding sample code to show a quick example of how this would work with Wendy?

levibostian commented 4 years ago

I will add to the docs eventually. Right now I am spending all of my development time on this task.

These official docs on workmanager are pretty good to help you setup workmanager. it does not take a lot of code.

When workmanager executes, your WorkManager Job class needs to run Wendy.shared.runTasks(null). That's all the Wendy specific code there is.

essieM commented 4 years ago

Thanks for the pointers. I think that gives me an idea of what's needed.

levibostian commented 4 years ago

Awesome. Enjoy Wendy!

Note: I do have a sample project made that uses Wendy and WorkManager in it: https://github.com/levibostian/AndroidBlanky The problem is that it's a pretty complex app. If you are using Dagger or Hilt in your app already, then AndroidBlanky will be beneficial for you to look over. But if your app is pretty simple then this project will be overkill for you to learn from.