rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
288 stars 328 forks source link

Robust approach #257

Closed rahuldange09 closed 3 years ago

rahuldange09 commented 3 years ago

I found this thing on android not sure about ios as I'm not currently using it on ios.

In some of the last releases, I can see that to check whether Location service is running or not you are taking the help of shared preferences instead of the normal "isRunning" variable.

Well, the problem is that with this approach I started facing some issues like "Service is already running even it's not running" sometimes.

So problem is that if you depend on shared preferences to check service is running or not then it's a bad approach. As sometimes service can be killed by the user or by OS in some cases and in that cases OS doesn't ensure that it will call "onDestroy" method of service (You can find the same discussions on google). So in such cases in Shared Preferences it will still remain value as "true" even when service is not running and that will result in able to start service at all.

As before you were dependent on a static variable named "isRunning", with that this wasn't really a problem as it defaults to false whenever code executes for the first time. But that is also not a full-proof option.

So, in that case, I think it's always better to check by this API way. I know it's deprecated but it still works way better than the current and last approaches as that deprecation is just for the third party.

So what's your thought on this?

mehdok commented 3 years ago

Hi @rahuldange09 Thank you for opening an issue.

I agree with you, using shared preferences is not a robust option, neither using a static variable. this plugin is supposed to work when the app is killed, in that case, there is nothing in memory related to that app, so static var would be useless. Considering that we have a foreground service running all the time (in Android), but this issue reported repeatedly.

About the way you suggested, I considered using that way a while ago, and yes that works perfectly, but I'm not sure about using deprecated API.

Someone suggested another way (in another issue) that night work, I'll try to implement that to see the result.

mehdok commented 3 years ago

I did a little modification and the problem on Android should be fixed by now. But there is still problem on iOS

rahuldange09 commented 3 years ago

Great so fast! A static variable can be better for now. I'm not sure about iOS, I don't use ios right now nor I understand swift that much but there has to be a way. Thank you

I guess with binding this might help you: Medium