Skip to content

Commit

Permalink
Not initialized bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
vanlooverenkoen committed Feb 18, 2021
1 parent be7d293 commit 45efd9e
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class SwiftBackgroundLocationTrackerPlugin: FlutterPluginAppLifeCycleDele
private static var flutterEngine: FlutterEngine? = nil
private static var hasRegisteredPlugins = false
private static var initializedBackgroundCallbacks = false
private static var initializedBackgroundCallbacksStarted = false
private static var locationData: [String: Any]? = nil

private static var flutterPluginRegistrantCallback: FlutterPluginRegistrantCallback?
Expand Down Expand Up @@ -61,23 +60,25 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin {

public static func initBackgroundMethodChannel(flutterEngine: FlutterEngine) {
if (backgroundMethodChannel == nil) {
let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger)
backgroundMethodChannel.setMethodCallHandler { (call, result) in
CustomLogger.log(message: "backgroundMethodChannel is nil, we will create a new background method channel.")
backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger)
backgroundMethodChannel!.setMethodCallHandler { (call, result) in
switch call.method {
case BackgroundMethods.initialized.rawValue:
CustomLogger.log(message: "Initialized ...")
initializedBackgroundCallbacks = true
let locationData = SwiftBackgroundLocationTrackerPlugin.locationData
if let data = locationData {
CustomLogger.log(message: "Initialized with a cached location, we will send it!")
sendLocationupdate(locationData: data)
} else {
CustomLogger.log(message: "Initialized without a cached location")
}
result(true)
default:
CustomLogger.log(message: "Not implemented method -> \(call.method)")
result(FlutterMethodNotImplemented)
}
}
self.backgroundMethodChannel = backgroundMethodChannel
}
}

Expand Down Expand Up @@ -121,15 +122,11 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate {
CustomLogger.log(message: "NOT YET INITIALIZED. Cache the location data")
SwiftBackgroundLocationTrackerPlugin.locationData = locationData

if (!SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted){
SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted = true

guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else {
CustomLogger.log(message: "No Flutter engine available ...")
return
}
SwiftBackgroundLocationTrackerPlugin.initBackgroundMethodChannel(flutterEngine: flutterEngine)
guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else {
CustomLogger.log(message: "No Flutter engine available ...")
return
}
SwiftBackgroundLocationTrackerPlugin.initBackgroundMethodChannel(flutterEngine: flutterEngine)
}
}
}

0 comments on commit 45efd9e

Please sign in to comment.