I'm working on a service for a Flutter app that should:
Check for a specific type of user data and send it to a server on app startup
Periodically, while the app is running, check for the same data and attempt to send it
Usually a single service execution sends up to 5-6 requests and takes about a minute
The service should be active only when the app is in the foreground, but it must not block the main thread so that the user can use the app normally during the sending attempts
Is it acceptable to implement such a service on the main isolate? Are there any reasons why it would be better to handle the data sending in a separate isolate or a background service?
The rule of thumb is that you should always run code on a separate isolate if it is both A) time-consuming and B) synchronous. If your code takes about a minute but the vast majority of that time is spent awaiting I/O requests, then it's fine to let it run on the main isolate. But if that time is spent synchronously gathering and processing user data, then it should be run on its own isolate so as to not block the UI thread.
But this is only the rule of thumb. Ultimately, the answer for your specific use-case is something only you can determine by actually trying it: run it on the main isolate, and if it causes the app to freeze up, then refactor it onto a separate isolate.
(And as far as a background service goes, you said that this will only be running when the app is in the foreground so that would be overkill.)
Abion47