Skip to content

Commit

Permalink
fix-iOS: Fixed an issue where main function was called repeatedly whe…
Browse files Browse the repository at this point in the history
…n there was no callback to start
  • Loading branch information
Dev-hwang committed Oct 4, 2024
1 parent db2abe4 commit 0def3ee
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions ios/Classes/service/ForegroundTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,47 @@ class ForegroundTask {
self.taskData = taskData
self.taskEventAction = taskEventAction
self.taskLifecycleListener = taskLifecycleListener
initialize()
}

private func initialize() {
guard let registerPlugins = SwiftFlutterForegroundTaskPlugin.registerPlugins else {
print("Please register the registerPlugins function using the SwiftFlutterForegroundTaskPlugin.setPluginRegistrantCallback.")
return
}

if let registerPlugins = SwiftFlutterForegroundTaskPlugin.registerPlugins {
// create flutter engine
let flutterEngine = FlutterEngine(name: BG_ISOLATE_NAME, project: nil, allowHeadlessExecution: true)
guard let callbackHandle = taskData.callbackHandle else {
// no callback -> Unlike Android, the flutter engine does not start.
return
}

// lookup callback
let callbackInfo = FlutterCallbackCache.lookupCallbackInformation(callbackHandle)
guard let entrypoint = callbackInfo?.callbackName else {
print("Entrypoint not found in callback information.")
return
}
guard let libraryURI = callbackInfo?.callbackLibraryPath else {
print("LibraryURI not found in callback information.")
return
}

// create flutter engine & execute callback
let flutterEngine = FlutterEngine(name: BG_ISOLATE_NAME, project: nil, allowHeadlessExecution: true)
let isRunningEngine = flutterEngine.run(withEntrypoint: entrypoint, libraryURI: libraryURI)

if isRunningEngine {
// register plugins
registerPlugins(flutterEngine)
taskLifecycleListener.onEngineCreate(flutterEngine: flutterEngine)

// lookup callback
var entrypoint: String? = nil
var libraryURI: String? = nil
if let callbackHandle = taskData.callbackHandle {
let callbackInfo = FlutterCallbackCache.lookupCallbackInformation(callbackHandle)
entrypoint = callbackInfo?.callbackName
libraryURI = callbackInfo?.callbackLibraryPath
}
// create background channel
let messenger = flutterEngine.binaryMessenger
let backgroundChannel = FlutterMethodChannel(name: BG_CHANNEL_NAME, binaryMessenger: messenger)
backgroundChannel.setMethodCallHandler(onMethodCall)

// run flutter engine & execute callback
let isRunningEngine = flutterEngine.run(withEntrypoint: entrypoint, libraryURI: libraryURI)
if isRunningEngine {
// register plugins
registerPlugins(flutterEngine)
taskLifecycleListener.onEngineCreate(flutterEngine: flutterEngine)

// create background channel
let messenger = flutterEngine.binaryMessenger
let backgroundChannel = FlutterMethodChannel(name: BG_CHANNEL_NAME, binaryMessenger: messenger)
backgroundChannel.setMethodCallHandler(onMethodCall)

self.flutterEngine = flutterEngine
self.backgroundChannel = backgroundChannel
}
} else {
print("Please register the registerPlugins function using the SwiftFlutterForegroundTaskPlugin.setPluginRegistrantCallback.")
self.flutterEngine = flutterEngine
self.backgroundChannel = backgroundChannel
}
}

Expand Down

0 comments on commit 0def3ee

Please sign in to comment.