Skip to content

Commit

Permalink
Make sure FlutterEventChannel is on the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
jasikpark committed Feb 21, 2025
1 parent a06476f commit ecc372d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
8 changes: 7 additions & 1 deletion ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ func MissingArgumentError(message: String, details: Any?) -> FlutterError {

Task {
for await site in await dnUpdater.siteUpdates {
self.sites?.updateSite(site: site)
// Signal the site has changed in case the current site details screen is active
let container = sites?.getContainer(id: site.id)
if container != nil {
// Update references to the site with the new site config
container!.site = site
container!.updater.update(connected: site.connected ?? false, replaceSite: site)
}

// Signal to the main screen to reload
self.ui?.invokeMethod("refreshSites", arguments: nil)
Expand Down
18 changes: 5 additions & 13 deletions ios/Runner/Sites.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class Sites {
case .failure(let error):
return Result.failure(error)
case .success(let sites):
sites.values.forEach { site in
for site in sites.values {
var updater = self.containers[site.id]?.updater
if updater != nil {
updater!.setSite(site: site)
await updater!.setSite(site: site)
} else {
updater = SiteUpdater(messenger: self.messenger!, site: site)
updater = await SiteUpdater(messenger: self.messenger!, site: site)
}
self.containers[site.id] = SiteContainer(site: site, updater: updater!)
}
Expand Down Expand Up @@ -84,18 +84,10 @@ class Sites {
return containers[id]
}

func updateSite(site: Site) {
// Signal the site has changed in case the current site details screen is active
let container = getContainer(id: site.id)
if container != nil {
// Update references to the site with the new site config
container!.site = site
container!.updater.update(connected: site.connected ?? false, replaceSite: site)
}
}
}

class SiteUpdater: NSObject, FlutterStreamHandler, @unchecked Sendable {
@MainActor
final class SiteUpdater: NSObject, FlutterStreamHandler, @unchecked Sendable {
private var eventSink: FlutterEventSink?
private var eventChannel: FlutterEventChannel
private var site: Site
Expand Down

0 comments on commit ecc372d

Please sign in to comment.