Skip to content

Commit

Permalink
fix: iOS background worker (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
armandsLa authored Jul 15, 2024
1 parent 1b0456f commit bb4895c
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions ios/Classes/HomeWidgetBackgroundWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public struct HomeWidgetBackgroundWorker {
static let dispatcherKey: String = "home_widget.internal.background.dispatcher"
static let callbackKey: String = "home_widget.internal.background.callback"

static var isSetupCompleted: Bool = false
static var engine: FlutterEngine?
static var channel: FlutterMethodChannel?
static var queue: [(URL?, String)] = []

static var isSetupCompleted: Bool = false
static var continuations: [CheckedContinuation<Void, Never>] = []

private static var registerPlugins: FlutterPluginRegistrantCallback?

Expand All @@ -30,15 +31,17 @@ public struct HomeWidgetBackgroundWorker {
/// The url you provide will be used as arguments in the callback function in dart
/// The AppGroup is necessary to retrieve the dart callbacks
static public func run(url: URL?, appGroup: String) async {
if isSetupCompleted {
let preferences = UserDefaults.init(suiteName: appGroup)
let dispatcher = preferences?.object(forKey: dispatcherKey) as! Int64
NSLog("Dispatcher: \(dispatcher)")

await sendEvent(url: url, appGroup: appGroup)
} else {
queue.append((url, appGroup))
if isSetupCompleted == false {
await withCheckedContinuation { continuation in
continuations.append(continuation)
}
}

let preferences = UserDefaults.init(suiteName: appGroup)
let dispatcher = preferences?.object(forKey: dispatcherKey) as! Int64
NSLog("Dispatcher: \(dispatcher)")

await sendEvent(url: url, appGroup: appGroup)
}

static func setupEngine(dispatcher: Int64) {
Expand Down Expand Up @@ -69,11 +72,9 @@ public struct HomeWidgetBackgroundWorker {
switch call.method {
case "HomeWidget.backgroundInitialized":
isSetupCompleted = true
while !queue.isEmpty {
let entry = queue.removeFirst()
Task {
await sendEvent(url: entry.0, appGroup: entry.1)
}
while !continuations.isEmpty {
let continuation = continuations.removeFirst()
continuation.resume()
}
result(true)
default:
Expand All @@ -82,16 +83,19 @@ public struct HomeWidgetBackgroundWorker {
}

static func sendEvent(url: URL?, appGroup: String) async {
DispatchQueue.main.async {
let preferences = UserDefaults.init(suiteName: appGroup)
let callback = preferences?.object(forKey: callbackKey) as! Int64

channel?.invokeMethod(
"",
arguments: [
callback,
url?.absoluteString,
])
guard let _channel = channel else {
return
}
let preferences = UserDefaults.init(suiteName: appGroup)
guard let _callback = preferences?.object(forKey: callbackKey) as? Int64 else {
return
}
await withCheckedContinuation { continuation in
DispatchQueue.main.async {
_channel.invokeMethod("", arguments: [_callback, url?.absoluteString]) { _ in
continuation.resume()
}
}
}
}
}

0 comments on commit bb4895c

Please sign in to comment.