Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background task doesn't run after a while[BUG] #23

Closed
SebastianRuettger opened this issue Apr 21, 2019 · 20 comments
Closed

Background task doesn't run after a while[BUG] #23

SebastianRuettger opened this issue Apr 21, 2019 · 20 comments

Comments

@SebastianRuettger
Copy link

Your Environment

  • Plugin version: 1.21
  • Platform: Android
  • OS version: 8.0.0
  • Device manufacturer / model: OnePlus 3T
  • Flutter info (flutter info, flutter doctor): Doctor summary (to see all details, run flutter doctor -v):
    [√] Flutter (Channel stable, v1.2.1, on Microsoft Windows [Version 10.0.17134.706], locale de-DE)
    [√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    [!] Android Studio (version 3.0)
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    [√] Android Studio (version 3.2)
    [√] VS Code (version 1.33.1)
    [!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.

  • Plugin config

To Reproduce
Steps to reproduce the behavior:

  1. BackgroundFetchConfig(
    minimumFetchInterval: 15,
    stopOnTerminate: false,
    startOnBoot: true,
    enableHeadless: true)
  2. call background task
  3. don't use the app for a day
@christocracy
Copy link
Member

The Android plugin uses JobScheduler. Jobs will be throttled based upon app usage and whether the device is plugged into power or not. You won't get events fired exactly every 15 minutes. They will be delayed.

@SebastianRuettger
Copy link
Author

That's what I thought too, but I then won't get any events for days.

@christocracy
Copy link
Member

If the user operates your app normally (ie opening it regularly), this shouldn’t be a problem.

@SebastianRuettger
Copy link
Author

The problem there is, that the usecase has the user opening the app every couple of days and then seeing all events

@christocracy
Copy link
Member

I suggest sending a server push to devices you haven’t heard from in a while.

@SebastianRuettger
Copy link
Author

That's what I was afraid of, thank you anyway

@christocracy
Copy link
Member

Are you simulating fetch events to make sure everything is “wired up” properly?

If simulated events run, nothing else to do. See README to learn how to simulate Android fetch events with adb

@SebastianRuettger
Copy link
Author

The events are called for a long time, but after about a day it just stops

@christocracy
Copy link
Member

And are you sure you're calling BackgroundFetch.finish() each time? If you fail to, Android will punish your app and fire events less frequently.

@SebastianRuettger
Copy link
Author

i have two calls to BackgroundFetch.finish() one in the background method and one after the call to the background method, just to be safe

@christocracy
Copy link
Member

i have two calls to BackgroundFetch.finish() one in the background method and one after the call to the background method, just to be safe

Show me what you mean by this.

@SebastianRuettger
Copy link
Author

SebastianRuettger commented Apr 23, 2019

void backgroundFetch() async {
  await fetchNewEntries(() {});
  BackgroundFetch.finish();
}
BackgroundFetch.configure(
        BackgroundFetchConfig(
            minimumFetchInterval: 15,
            stopOnTerminate: false,
            startOnBoot: true,
            enableHeadless: true), () async {
      fetchNewEntries(() {
        setState(() {});
      }).then((_) {
        setState(() {});
        BackgroundFetch.finish();
      });
    }).then((int status) {
      print("status: $status");
      setState(() {});
    });

i know the second one looks horrible, all the calls to setstate are more for testing

@christocracy
Copy link
Member

syntax-highlighting multiple lines requires 3 backticks, not one:

@christocracy
Copy link
Member

Actually, forget what I said above, re (//<--- Pointless). I see that's in the callback to your #fetchNewEntries method, which is correct.

@christocracy
Copy link
Member

christocracy commented Apr 23, 2019

Are you simulating background-fetch events with adb? See the README to see how. Are the simulated fetch events firing? If so, I think it's the purpose of Android JobScheduler: If the user doesn't interact with the app for a long time, JobScheduler events are throttled.

@SebastianRuettger
Copy link
Author

i havent simulated events, but background events work´for a while

@christocracy
Copy link
Member

And are you opening the app daily, like a regular user would?

@SebastianRuettger
Copy link
Author

No, I keep forgetting, but my usecase would have the user opening the app only every couple of days anyway

@christocracy
Copy link
Member

I suggest you behave as a “regular user” and observe the behavior over a week or two. Hopefully you’re sending an http request to your server as evidence an event occurs.

There’s nothing else to do.

@SebastianRuettger
Copy link
Author

i log the calls in sqflite since the http request is to a server i dont own

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants