rekabhq / background_locator

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

Question about initCallback and initDataCallback #227

Closed TheSuperiorStanislav closed 3 years ago

TheSuperiorStanislav commented 3 years ago

In version 1.6.0+1-beta , initCallback and initDataCallback were removed. Is it intentional? If so what is replacement(probably wiki should be updated)

void startLocationService(){
    BackgroundLocator.registerLocationUpdate(LocationCallbackHandler.callback,
        initCallback: LocationCallbackHandler.initCallback,
        initDataCallback: data,
        disposeCallback: LocationCallbackHandler.disposeCallback,
        autoStop: false,
        iosSettings: IOSSettings(
            accuracy: LocationAccuracy.NAVIGATION, distanceFilter: 0),
        androidSettings: AndroidSettings(
            accuracy: LocationAccuracy.NAVIGATION,
            interval: 5,
            distanceFilter: 0,
            androidNotificationSettings: AndroidNotificationSettings(
                notificationChannelName: 'Location tracking',
                notificationTitle: 'Start Location Tracking',
                notificationMsg: 'Track location in background',
                notificationBigMsg:
                    'Background location is on to keep the app up-tp-date with your location. This is required for main features to work properly when the app is not running.',
                notificationIcon: '',
                notificationIconColor: Colors.grey,
                notificationTapCallback:
                    LocationCallbackHandler.notificationCallback)));
}
mehdok commented 3 years ago

Hi @TheSuperiorStanislav Thank you for opening an issue.

Yes, that was intentional.

Every function (initialize, registerLocationUpdate, unRegisterLocationUpdate) is an async function, so for example, if you need to know where the initialization is finished just call it with the await keyword and it will hold you until initialization is finished, the same thing applies to the dispose callback.

About initDataCallback, if you need to store any value just put them in the preferences, and whenever you need it just retrieve it.

TheSuperiorStanislav commented 3 years ago

@mehdok Thank you for responding. I have a few questions About other plugin usages. The wiki stated that we need to add this line of code but in the new version: IsolateHolderService doesn't have such a method. It seems that it works without.

IsolateHolderService.setPluginRegistrant(this)

On ios sometimes logger shows. The locator works, but is it something I should be worried about?

[....] Disconnected 7c593290f7aaea8c417705af92c4baafc18e3b47

From time to time the whole app crashes with this exception when I starting the locator. It's like one time okay, the other not. (On android 11(simulator) and 9(device))

E/AndroidRuntime(13499): java.lang.RuntimeException: Unable to start service rekab.app.background_locator.IsolateHolderService@601cf53 with Intent { act=SHUTDOWN cmp=com.saritasa.workapp.dev/rekab.app.background_locator.IsolateHolderService }: kotlin.UninitializedPropertyAccessException: lateinit property locatorClient has not been initialized
E/AndroidRuntime(13499):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4319)
E/AndroidRuntime(13499):    at android.app.ActivityThread.access$2700(ActivityThread.java:273)
E/AndroidRuntime(13499):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2070)
E/AndroidRuntime(13499):    at android.os.Handler.dispatchMessage(Handler.java:112)
E/AndroidRuntime(13499):    at android.os.Looper.loop(Looper.java:216)
E/AndroidRuntime(13499):    at android.app.ActivityThread.main(ActivityThread.java:7625)
E/AndroidRuntime(13499):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(13499):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
E/AndroidRuntime(13499):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
E/AndroidRuntime(13499): Caused by: kotlin.UninitializedPropertyAccessException: lateinit property locatorClient has not been initialized
E/AndroidRuntime(13499):    at rekab.app.background_locator.IsolateHolderService.shutdownHolderService(IsolateHolderService.kt:161)
E/AndroidRuntime(13499):    at rekab.app.background_locator.IsolateHolderService.onStartCommand(IsolateHolderService.kt:117)
E/AndroidRuntime(13499):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4298)
E/AndroidRuntime(13499):    ... 8 more
allanguintu commented 3 years ago

Hi,

I am passing a variable (firebase uid) to my isolate on initDataCallback, everything works fine before. But now that the initDataCallback is removed, how do I pass my variable? Looking forward for your help. Example code is also not updated. Thank you.

RomanJos commented 3 years ago

Hello @mehdok I didn't quite understood your explanation, thoses init parameters are a way to set things up in the isolate, for example I use initData to send the app Documents path, and initCallback to create a File instance which can then be written the newly received locations.

I remember we were having a conversation about the way memory gets separated when stuff is in isolate and you didn't undertstood the concept ; https://github.com/rekab-app/background_locator/issues/31#issuecomment-611325337 is totally wrong for example. I thought you got it with @gpibarra's answer but this is the proof you didn't :(

This is the only library that offer a callback ran in the background's own thread, initData and init Callback were the cherry on top.

I would be really happy if you could bring those functions again

mehdok commented 3 years ago

Hi @TheSuperiorStanislav

Thank you for your patient. The Wiki needs to be updated and I will do it asap. Right now in Android there is no need to register 3rd party plugins like before, but for iOS please follow the wiki;

mehdok commented 3 years ago

Hi @allanguintu Thank you for your patient.

Please save your variable in ShredPreferences and access it anywhere you like, even in the callback.

mehdok commented 3 years ago

Hi @RomanJos Long time no see :)

I understand your concern and I remember our conversation about this, but there is a simpler option for this, Using ShredPreferences. Back then we didn't have any way to use 3rd party library in the callback, right now you can use ShredPreferences to store any value and use it anywhere you like, even in callback;

RomanJos commented 3 years ago

lol yeah I was away from coding a little bit 😅

Concerning this issue, if I understand correctly, in my case I need to check on every callback if my File Instance from the isolate isn't null to set it by reading SharedPreference ? That seems a lot of unnecessary work, having just a initCallback would allow me to create the File instance the first time and leaving the callback doing its main work instead of checking if it need to be initialized everytime

The current way to spawn an isolate in dart is limited to only one argument so initDataCallback made sense for me, its weird to save variable to a file instead of passing it as argument

mehdok commented 3 years ago

246

RomanJos commented 3 years ago

Awesome !!