rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
287 stars 321 forks source link

[iOS 🐛] Compatibility with other plugin #316

Open lulupointu opened 2 years ago

lulupointu commented 2 years ago

The issue

I'm developing a plugin which uses a native SDK to operate. The issue is that when adding my plugin and background_locator to an app, the native SDK of my plugin can no longer communicate with my dart code.

After investigating, is seems that the problematic part is the swift code you ask to add during initialization:

BackgroundLocatorPlugin.setPluginRegistrantCallback { registry in
    if (!registry.hasPlugin("BackgroundLocatorPlugin")) {
        GeneratedPluginRegistrant.register(with: registry)
    } 
}

The issue is that this code calls registerWithRegistar on my plugin, with the registrar you are giving (which is a FlutterEngine). It appears that channels created with this registrar are broken (I managed to reproduce the issue with thequick_actions plugin as well).

Question 1:

Can you explain the reason why do you recommend adding this code when using the background_locator plugin? Especially since, it seems like any other call to registerWithRegistrar occurring after the 1st one is ignored according to the following code:

+ (void)registerWithRegistrar:(nonnull NSObject<FlutterPluginRegistrar> *)registrar {
    @synchronized(self) {
        if (instance == nil) { // False during the call to `GeneratedPluginRegistrant.register(with: registry)`since `GeneratedPluginRegistrant.register(with: self)` was already called
            instance = [[BackgroundLocatorPlugin alloc] init:registrar];
            [registrar addApplicationDelegate:instance];
        }
    }
}

Question 2:

In the documentation "Use other plugins in callback" you recommend using the following code for other plugins:

if !hasPlugin("io.flutter.plugins.pathprovider") {
    FLTPathProviderPlugin
        .register(with: registrar(forPlugin: "io.flutter.plugins.pathprovider"))
}

Is there a reason the setup of background_locator and the setup of additional plugin is different ? The first makes us register a second time ALL plugins, while the second only registers background_locator. Should we replace the setup section with the following :

BackgroundLocatorPlugin.setPluginRegistrantCallback { registry in
    if (!registry.hasPlugin("BackgroundLocatorPlugin")) {
        BackgroundLocatorPlugin.register(with: registry.registrar(forPlugin: "BackgroundLocatorPlugin")!)
    }
}
Yukams commented 2 years ago

Hi, have you found any answers to your questions ? I feel like I can't really use other plugins too

lulupointu commented 2 years ago

Hi, unfortunately not. I spent a lot of time digging into this problem and this issue was the last call for help (which went on unanswered as you can see 😉)

Yukams commented 2 years ago

Sorry to hear that, thanks for the update :)