background_locator: unRegisterLocationUpdate don't stop the Isolate

Hi I have some issue with the plugins not stopping the Isolate, I’m creating a new file with a new name every time in a specific directory before launching the plugins and the plugins retrieve this file by listing every files in this directory (I store the date and a string in the name to not creating a special db for this), the file is stored in static File _currentFile; at the top of the class to not list the directory every callback. When its finished I stop the plugin using

BackgroundLocator.unRegisterLocationUpdate();
IsolateNameServer.removePortNameMapping(_isolateName);

I copy the file and delete it in the directory. When I recreate another file and re-launch the plugin it will see that _currentFile is not null and write to it even though its not the file I want.

I tried the example and its the same, by clicking stop I still see the plugin’s thread running in VScode

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 22 (11 by maintainers)

Most upvoted comments

@gpibarra Sorry for late answer. Your idea is really good and it’s working. But I think this is a complicated solution for simple problem. As I said in my previous reply the solution to this problem looks very simple to me and I tested that on Roman example and it’s working.

@gpibarra I saw your code, and honestly I don’t get the point of it.

This is my understanding of your code, correct me if I’m wrong.

  1. You created initCallback and disposeCallback, the init has `initialDataMap
  2. When plugin starts to work you call initCallback and pass it’s saved data to flutter app
  3. When plugin stops you call disposeCallback

When plugin start and call “initCallback” dont pass data to flutter app. Data always stays in the class facade inside isolate. Currently, sending data through the port, just for the purpose of refreshing the UI. But it is optional.

What is not clear for me:

  1. How this can help with @RomanJos problem? He just want to use some static variable inside callback, he is not trying to send anything to background service?
  2. When you call BackgroundLocator.registerLocationUpdate it’s the point of starting plugin, So why we need separate initCallback? and whats the point of initialDataMap? You don’t do any process on it inside plugin, it just get saved and passed back.

Both initCallback and disposeCallback, executed within the isolation process, are to handle the state of the facade class that contains variables necessary for the userand defined by the user

  1. Same question about initCallback applies for disposeCallback. Whats the point of it? When user call BackgroundLocator.unRegisterLocationUpdate() isn’t it the dispose?

In the example, a simple counter is implemented, the “count” variable is always in the facade class inside isolate. It is not a static variable that is visible from the mail proccess. The only communication will always be through ports (maybe it can be shared preferences, db or some other method). If you don’t do an initCallback, the counter will continue to add indefinitely even if the user calls BackgroundLocator.unRegisterLocationUpdate(). So when calling BackgroundLocator.registerLocationUpdate() the count variable returns 0. In this case the dispose function does nothing, but in other cases it can do. Applied to the @RomanJos problem, when call initCalback, in initData it passes path file to save. In disposeCallback, the file is moved to another directory. In other cases, for example consuming an api, in initCallback a JWT token is negotiated (with dataInit user and pass). This cannot be done with static variables, if static variables are set in the main process, the first time is executed isolation the statics variables are empty. And in disposeCallback send to the api that has finished capturing locations, for example.

I am unsure if the parameter should send it when the user calls BackgroundLocator.unRegisterLocationUpdate() or when he calls BackgroundLocator.registerLocationUpdate(). Definitely the dispose will be executed when calling BackgroundLocator.unRegisterLocationUpdate() but I finally decided to pass it to the start. I still doubt about this

Making the isolation process store information in variables and have some business logic, is applied in cases where business logic cannot always be in the main process (communicated with ports) because the main process may not be running and if running the isolation process.