Secondary thread on flutter

Asked

Viewed 351 times

5

Hello, I have the following problem in flutter: I currently have an application with the plugin flutter_background_geolocation based on the example that is provided by the plugin. While performing some tests and analyzing the results my teacher requested that I fire two threads with the plugin and implement on the server side a logic to read only one at a time(the second thread would be a reserve for in case the first one fails) the sending and receiving logic has already been implemented. The question that stuck with me was how to implement a second thread in the flutter with that I started researching and I got the following questions:

1) Would the use of omissions be enough to carry out this action? I read that they are not suitable for lengthy things because they are not separate threads but use a system of Event loop similar to the Node.

2) I found in the documentation a function called Compute, but I was in doubt about its implementation and if it would meet me, all the examples I saw of it performed short actions (few seconds) and I need something that will run indefinitely.

3) It would be the use of Platform Channels an option? I believe that is the most complicated but the most robust option.

4) The use of a second thread itself is a good option to ensure the sending of the data? what other possible solutions?

  • 1

    Do an analysis of possible problems to see if it is really necessary to use two threads... Two threads running, is 2x more resource use, is 2x more processing use. What are the reasons for one of them failing? How to ensure that if one fails, the other also will not fail for the same reason? I find it more valid you do very well a control in a single thread than having two with the same purpose.

  • It was what the teacher responsible for the project requested, I already corrected the mistakes that made fail but he still thinks necessary

1 answer

1

Asynchronous programming in Flutter is performed through the Future. I don’t understand why you use two Future. If the idea is that the second is a backup in case of an error, you can use the Future API itself to handle the errors.

In the example below, if you comment on the line throw 'error'; it prints "Ok 1". If leave as is, will print "Ok 2".

 Future<String> primeiraChamada() async {
    throw 'error';
    return Future.delayed(Duration(seconds:1), () => 'ok 1');
  }
  
  Future<String> segundaChamada() async {
    return Future.delayed(Duration(seconds:1), () => 'ok 2');
  }
  
  void fazerChamadaAPI(){
    primeiraChamada()
      .then((result) => print(result))
      .catchError((error) => segundaChamada()
             .then((result) => print(result))
             .catchError((error) => print(error))
      );
  }
  
  fazerChamadaAPI();
  1. Yes. You use the type Future precisely for that reason.

  2. You don’t have to. compute is only used to start isolates and even in these cases have better ways of doing. https://api.dart.dev/stable/2.8.4/dart-isolate/Isolate-class.html

  3. Platform Channels not for that. They’re used to create code in native Sdks when you don’t have an option inside Flutter. You can use to create threads, but doing in Dart is much easier and with better performance.

  4. It was the first question I had when reading the question. The best way is to treat the response of the first call, or the error, and see what to do from there (create another call, schedule to send later, save location, etc)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.