Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/abtesting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
-scheme 'ABTestingExample (iOS)' \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=iPhone 11' \
build \
test \
| xcpretty
env:
Expand Down
27 changes: 27 additions & 0 deletions abtesting/Shared/AppConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ class AppConfig: ObservableObject {
}
}

#if swift(>=5.5)
@available(iOS 15, *)
func updateFromRemoteConfigAsync() async {
let remoteConfig = RemoteConfig.remoteConfig()
do {
let status = try await remoteConfig.fetch(withExpirationDuration: 0)
print("Config fetch completed with status: \(status.debugDescription)")
do {
let changed = try await remoteConfig.activate()
let value = remoteConfig["color_scheme"].stringValue ?? "nil"
if changed {
print("Remote Config changed to: \(value)")
DispatchQueue.main.async {
Copy link
Contributor

@peterfriese peterfriese Jul 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to use @MainActor to ensure colorScheme is updated on the main actor?

Using DispatchQueue.main.async in combination with the new async/await features feels a bit like an anti-pattern to me.

See https://peterfriese.dev/swiftui-concurrency-essentials-part1/#updating-published-properties

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, thanks! I will include this change in a separate PR 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, is there an alternative other than using @MainActor? Since we need the app to be backward-compatible with previous versions of iOS, I am not sure how to conditionally add @MainActor...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of options:

  1. You could apply it to a function that needs to run on the main actor (and the #ifdef that function)
  2. Add it to the class (and #ifdef it there)
  3. Call MainAction.run directly: https://www.avanderlee.com/swift/mainactor-dispatch-main-thread/#using-the-main-actor-directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, in that case I can apply it to the already conditionally-compiled async function. Thanks for the pointers!

self.colorScheme = ColorScheme(value)
}
} else {
print("Remote Config did not change from: \(value)")
}
} catch {
print("Error activating config: \(error)")
}
} catch {
print("Error fetching config: \(error)")
}
}
#endif

@objc func printInstallationAuthToken() {
Installations.installations().authTokenForcingRefresh(true) { token, error in
if let error = error {
Expand Down
14 changes: 9 additions & 5 deletions abtesting/Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ struct ContentView: View {
var body: some View {
NavigationView {
VStack {
if #available(iOS 15, *) {
BasicList(data: data).refreshable {
appConfig.updateFromRemoteConfig()
}
} else { BasicList(data: data) }
#if swift(>=5.5)
if #available(iOS 15, *) {
BasicList(data: data).refreshable {
await appConfig.updateFromRemoteConfigAsync()
}
} else { BasicList(data: data) }
#else
BasicList(data: data)
#endif
Button("Refresh") { appConfig.updateFromRemoteConfig() }
Spacer()
}
Expand Down