starkdmi / download_manager

Isolated download manager with progress, cancellation, pause and resume
https://pub.dev/packages/isolated_download_manager
BSD 3-Clause "New" or "Revised" License
15 stars 2 forks source link

is it working in the background ? #1

Closed ondanplatform closed 1 year ago

starkdmi commented 1 year ago

Running in background mode on iOS and Android while Flutter application is terminated is not supported. The base Isolates class is used under the hood. You may need to use something like WorkManager.

ondanplatform commented 1 year ago

Actually, I meant not terminated. just in the background. [https://youtu.be/ozpaRFma0_E]

starkdmi commented 1 year ago

The package doesn't actually relies on the Flutter, it's a pure Dart implementation. As for Flutter, it seems additional work is required for application to continue running while in inactive or background modes on iOS.

starkdmi commented 1 year ago

@ondanplatform, I've tested the package on iOS and the downloading continues in inactive and background modes, even the UI is updated. The Debug and Profile mode works. You can confirm that on your devices:

git clone https://github.com/starkdmi/flutter_download_manager.git
cd example/complex
flutter pub get
flutter run --profile

But be aware that iOS can kill the application at any moment, in case of insufficient memory for example.

shahmirzali49 commented 1 year ago

@starkdmi I tested your example folder sample too. It's continuing. but my multiple downloading use case does not continue 😅. just stop and when I switch back to the app later the downloading progress continues.

I know it's not an issue or problem with the package but I wonder why it continues for one file downloading in your example folder sample but multiple downloads not.

my screen recording: https://youtu.be/MHbhtYT1FMc

I tested with Text widget for seeing the progress number for confirming the progress number stopping or not. I can confirm progress stopping.

I took a screenshot quick as possible ).

IMG_0135

IMG_0136

starkdmi commented 1 year ago

@Shahmirzali-Huseynov, I see that the progress is not updated in inactive mode, but I can't say for sure if the downloading continues or not on your screen recording. That's only possible to check via logs in console.

Not sure if the example application has some additional Xcode setup, but there are some flags or settings which may cause it on iOS. You may try to paste your code to the example app to confirm.

Still there is a possibility that iOS does not provide the internet access to the app running in the background when the wifi signal is bad, or the power is low, or anything else.

shahmirzali49 commented 1 year ago

@starkdmi I don't know why now your example does not continue in the background too 😅.

Anyway, I think this package doesn't offer running in the background(not terminated). but it would be nice if. cause I don't wanna mess with background download packages. I nearly implemented downloading with your packages.

shahmirzali49 commented 1 year ago

@starkdmi did you see this? : https://medium.com/flutter/introducing-background-isolate-channels-7a299609cad8

starkdmi commented 1 year ago

@Shahmirzali-Huseynov, can you confirm your Dart code is running in the background but the package stops functioning? Could you try to log some stuff to the console and see?

The package is pure Dart, so, either the Isolates not allowed in the background or the http package stops working, but It doesn't seem to be true...

As for the link to Medium you just sent - it's about the possibility to call native Swift/Kotlin code from Isolates.

shahmirzali49 commented 1 year ago

@starkdmi in Flutter Debug console(I'm using vs code) not showing logs when you run release in a real device. do you know another method for seeing logs?

starkdmi commented 1 year ago

Try logger_flutter

shahmirzali49 commented 1 year ago

fist of all, sorry to bother you.

it's not about packages. debug console not showing logs/prints for release mode. just in debug mode for ios.

I investigate, maybe Dart code is not working in this situation.

in this situation: when an application is in the "background", it is not actively in use or visible on the screen. This might happen when a user switches to another app, returns to the home screen, or when the device's screen turns off. I

starkdmi commented 1 year ago

@Shahmirzali-Huseynov, yeah, the logger package can log to file or db while __flutter_logger__ is a UI widget to view it right in your application on a real device. Another option is to run in Profile mode then.

shahmirzali49 commented 1 year ago

I run profile mode. it's an example of when I go home screen and back to the app after a while.

the logs except for flutter: Completed: progress % when I back to the app after a while.

first :

dnssd_clientstub read_all(18) DEFUNCT
[VERBOSE-2:FlutterObservatoryPublisher.mm(130)] Could not register as server for FlutterObservatoryPublisher. Check your network settings and relaunch the application.
dnssd_clientstub read_all(27) DEFUNCT
[VERBOSE-2:FlutterObservatoryPublisher.mm(130)] Could not register as server for FlutterObservatoryPublisher. Check your network settings and relaunch the application.
[connection] nw_read_request_report [C3] Receive failed with error "Software caused connection abort"
[connection] nw_read_request_report [C2] Receive failed with error "Software caused connection abort"

second :

[VERBOSE-2:profiler_metrics_ios.mm(203)] Error retrieving thread information: (os/kern) invalid argument
flutter: Completed: 28.32%
dnssd_clientstub write_all(34) DEFUNCT
dnssd_clientstub deliver_request ERROR: write_all(34, 81 bytes) failed
dnssd_clientstub write_all(34) DEFUNCT

all:

flutter: Completed: 0.35%
flutter: Completed: 0.70%
flutter: Completed: 1.05%
flutter: Completed: 1.40%
flutter: Completed: 1.75%
flutter: Completed: 2.10%
flutter: Completed: 2.45%
flutter: Completed: 2.80%
flutter: Completed: 3.15%
flutter: Completed: 3.50%
flutter: Completed: 3.85%
flutter: Completed: 4.20%
flutter: Completed: 4.55%
flutter: Completed: 4.90%
flutter: Completed: 5.24%
flutter: Completed: 5.59%
flutter: Completed: 5.94%
flutter: Completed: 6.29%
flutter: Completed: 6.64%
flutter: Completed: 6.99%
flutter: Completed: 7.34%
flutter: Completed: 7.69%
flutter: Completed: 8.04%
flutter: Completed: 8.39%
flutter: Completed: 8.74%
flutter: Completed: 9.09%
flutter: Completed: 9.44%
flutter: Completed: 9.79%
flutter: Completed: 10.14%
flutter: Completed: 10.49%
flutter: Completed: 10.84%
dnssd_clientstub read_all(18) DEFUNCT
[VERBOSE-2:FlutterObservatoryPublisher.mm(130)] Could not register as server for FlutterObservatoryPublisher. Check your network settings and relaunch the application.
dnssd_clientstub read_all(27) DEFUNCT
[VERBOSE-2:FlutterObservatoryPublisher.mm(130)] Could not register as server for FlutterObservatoryPublisher. Check your network settings and relaunch the application.
[connection] nw_read_request_report [C3] Receive failed with error "Software caused connection abort"
[connection] nw_read_request_report [C2] Receive failed with error "Software caused connection abort"
flutter: Completed: 11.19%
flutter: Completed: 11.54%
flutter: Completed: 11.89%
flutter: Completed: 12.24%
flutter: Completed: 12.59%
flutter: Completed: 12.94%
flutter: Completed: 13.29%
flutter: Completed: 13.64%
flutter: Completed: 13.99%
flutter: Completed: 14.34%
flutter: Completed: 14.69%
flutter: Completed: 15.03%
flutter: Completed: 15.38%
flutter: Completed: 15.73%
flutter: Completed: 16.08%
flutter: Completed: 16.43%
flutter: Completed: 16.78%
flutter: Completed: 17.13%
flutter: Completed: 17.48%
flutter: Completed: 17.83%
flutter: Completed: 18.18%
flutter: Completed: 18.53%
flutter: Completed: 18.88%
flutter: Completed: 19.23%
flutter: Completed: 19.58%
flutter: Completed: 19.93%
flutter: Completed: 20.28%
flutter: Completed: 20.63%
flutter: Completed: 20.98%
flutter: Completed: 21.33%
flutter: Completed: 21.68%
flutter: Completed: 22.03%
flutter: Completed: 22.38%
flutter: Completed: 22.73%
flutter: Completed: 23.08%
flutter: Completed: 23.43%
flutter: Completed: 23.78%
flutter: Completed: 24.13%
flutter: Completed: 24.48%
flutter: Completed: 24.83%
flutter: Completed: 25.17%
flutter: Completed: 25.52%
flutter: Completed: 25.87%
flutter: Completed: 26.22%
flutter: Completed: 26.57%
flutter: Completed: 26.92%
flutter: Completed: 27.27%
flutter: Completed: 27.62%
flutter: Completed: 27.97%
[VERBOSE-2:profiler_metrics_ios.mm(203)] Error retrieving thread information: (os/kern) invalid argument
flutter: Completed: 28.32%
dnssd_clientstub write_all(34) DEFUNCT
dnssd_clientstub deliver_request ERROR: write_all(34, 81 bytes) failed
dnssd_clientstub write_all(34) DEFUNCT
flutter: Completed: 28.67%
flutter: Completed: 29.02%
flutter: Completed: 29.37%
flutter: Completed: 29.72%
flutter: Completed: 30.07%
flutter: Completed: 30.42%
flutter: Completed: 30.77%
flutter: Completed: 31.12%
flutter: Completed: 31.47%
flutter: Completed: 31.82%
flutter: Completed: 32.17%
flutter: Completed: 32.52%
flutter: Completed: 32.87%
flutter: Completed: 33.22%
flutter: Completed: 33.57%
flutter: Completed: 33.92%
flutter: Completed: 34.27%
flutter: Completed: 34.62%
flutter: Completed: 34.97%
flutter: Completed: 35.31%
flutter: Completed: 35.66%
flutter: Completed: 36.01%
flutter: Completed: 36.36%
flutter: Completed: 36.71%
flutter: Completed: 37.06%
dnssd_clientstub write_all(34) DEFUNCT
dnssd_clientstub deliver_request ERROR: write_all(34, 81 bytes) failed
dnssd_clientstub write_all(34) DEFUNCT
flutter: Completed: 37.41%
flutter: Completed: 37.76%
flutter: Completed: 38.11%
flutter: Completed: 38.46%
flutter: Completed: 38.81%
flutter: Completed: 39.16%
flutter: Completed: 39.51%
flutter: Completed: 39.86%
flutter: Completed: 40.21%
flutter: Completed: 40.56%
flutter: Completed: 40.91%
flutter: Completed: 41.26%
flutter: Completed: 41.61%
flutter: Completed: 41.96%
flutter: Completed: 42.31%
flutter: Completed: 42.66%
flutter: Completed: 43.01%
flutter: Completed: 43.36%
flutter: Completed: 43.71%
flutter: Completed: 44.06%
flutter: Completed: 44.41%
flutter: Completed: 44.76%
flutter: Completed: 45.10%
flutter: Completed: 45.45%
flutter: Completed: 45.80%
flutter: Completed: 46.15%
flutter: Completed: 46.50%
flutter: Completed: 46.85%
flutter: Completed: 47.20%
flutter: Completed: 47.55%
flutter: Completed: 47.90%
shahmirzali49 commented 1 year ago

by the way when I go to home screen the progress log stopped @starkdmi

starkdmi commented 1 year ago

The network errors are related to Flutter <-> Code Editor I guess. Could you also try to call the Timer.periodic() and print something else?

shahmirzali49 commented 1 year ago

I wrote Timer.periodic. but both of them not worked or printed in the console. stopped. when I back to app then worked

 void startDownloads() async {
    final downloader = DownloadManager.instance;
    await downloader.init(isolates: 5);

    Timer.periodic(Duration(seconds: 2), (Timer t) {
      print('Current time: ${DateTime.now()}');
    });

    int ayahCount = 286;
    List<String> urls = [];
    for (var i = 1; i <= ayahCount; i++) {
      urls.add('https://verses.quran.com/Alafasy/mp3/${getFormattedNumber(2)}${getFormattedNumber(i)}.mp3');
    }

    setState(() => _progress = 0.0);

    var completed = 0, failed = 0;
    for (var url in urls) {
      .........
      .......
      ........
  }
flutter: Completed: 0.35%
flutter: Completed: 0.70%
flutter: Completed: 1.05%
flutter: Completed: 1.40%
flutter: Completed: 1.75%
flutter: Completed: 2.10%
flutter: Completed: 2.45%
flutter: Completed: 2.80%
flutter: Completed: 3.15%
flutter: Completed: 3.50%
flutter: Completed: 3.85%
flutter: Completed: 4.20%
flutter: Completed: 4.55%
flutter: Completed: 4.90%
flutter: Completed: 5.24%
flutter: Completed: 5.59%
flutter: Current time: 2023-06-16 00:01:51.127280
flutter: Completed: 5.94%
flutter: Completed: 6.29%
flutter: Completed: 6.64%
flutter: Completed: 6.99%
flutter: Completed: 7.34%
flutter: Completed: 7.69%
flutter: Completed: 8.04%
flutter: Completed: 8.39%
flutter: Completed: 8.74%
flutter: Completed: 9.09%
flutter: Completed: 9.44%
flutter: Completed: 9.79%
flutter: Current time: 2023-06-16 00:03:54.824932
flutter: Completed: 10.14%
dnssd_clientstub write_all(33) DEFUNCT
dnssd_clientstub deliver_request ERROR: write_all(33, 81 bytes) failed
dnssd_clientstub write_all(33) DEFUNCT
flutter: Completed: 10.49%
flutter: Completed: 10.84%
flutter: Completed: 11.19%
flutter: Completed: 11.54%
flutter: Current time: 2023-06-16 00:03:55.126667
flutter: Completed: 11.89%
flutter: Completed: 12.24%
flutter: Completed: 12.59%
flutter: Completed: 12.94%
flutter: Completed: 13.29%
flutter: Completed: 13.64%
flutter: Completed: 13.99%
flutter: Completed: 14.34%
flutter: Completed: 14.69%
flutter: Completed: 15.03%
flutter: Completed: 15.38%
flutter: Completed: 15.73%
flutter: Completed: 16.08%
flutter: Completed: 16.43%
flutter: Completed: 16.78%
flutter: Completed: 17.13%
flutter: Completed: 17.48%
flutter: Completed: 17.83%
flutter: Completed: 18.18%
flutter: Completed: 18.53%
flutter: Completed: 18.88%
flutter: Completed: 19.23%
flutter: Completed: 19.58%
flutter: Completed: 19.93%
flutter: Completed: 20.28%
flutter: Current time: 2023-06-16 00:03:57.127024
flutter: Completed: 20.63%
flutter: Completed: 20.98%
flutter: Completed: 21.33%
flutter: Completed: 21.68%
flutter: Completed: 22.03%
flutter: Completed: 22.38%
flutter: Completed: 22.73%
flutter: Completed: 23.08%
flutter: Completed: 23.43%
flutter: Completed: 23.78%
flutter: Completed: 24.13%
flutter: Completed: 24.48%
flutter: Completed: 24.83%
flutter: Completed: 25.17%
flutter: Completed: 25.52%
flutter: Completed: 25.87%
starkdmi commented 1 year ago

@Shahmirzali-Huseynov, I checked it once again, the downloading continues in background, while the logs not updated even in Debug mode. I tried same in simulator, started the downloading process, switched to another application and in about a minute I can see a 100MB file on file system. Then I re-opened the app and the logs were updated. To also print the app state use this.

One more link to Apple documentation of background execution - here.

shahmirzali49 commented 1 year ago

can I use this package with WorkManager or other background packages ?

starkdmi commented 1 year ago

@Shahmirzali-Huseynov I see no limitations, it's a pure Dart code.

shahmirzali49 commented 1 year ago

@starkdmi what do you think about background workers?

https://dart.dev/language/concurrency#background-workers

starkdmi commented 1 year ago

@Shahmirzali-Huseynov, they are the basis of this package - link.

shahmirzali49 commented 1 year ago

https://medium.com/fluttervn/run-your-code-in-background-easily-with-flutter-2e98d94f30de

https://github.com/fluttervn/isolate_woker

starkdmi commented 1 year ago

@Shahmirzali-Huseynov, there is one way to achieve background execution in Dart - Isolates. In Flutter there is a simplified version of Isolate.spawn() which is the compute() method, but its Isolate isn't reused by multiple calls.

shahmirzali49 commented 1 year ago

Hmm,i got you. Thanks for the explanation.