Post

Replies

Boosts

Views

Activity

Reply to SwiftData does not work on a background Task even inside a custom ModelActor.
The bug is in SwiftData’s ModelContext initializer implementation. The initializer checks whether it is running on the main dispatch queue and if so, configures the context as a main context rather than a background context. More details in FB13399899 (Open Radar ID 5518888167014400). Here is my workaround: extension ModelContext { struct UncheckedSendableWrapper: @unchecked Sendable { let modelContext: ModelContext } /// Creates a background context. /// /// - Remark: This method works around FB13399899, which causes `init(_:)` to sometimes configure the instance /// as a main context. This method is marked `async` and not `@MainActor`, which guarantees that the method will be /// called off of the main thread, working around the bug in the initializer implementation. static func makeBackgroundContext(for container: ModelContainer) async -> UncheckedSendableWrapper { let modelContext = ModelContext(container) return UncheckedSendableWrapper(modelContext: modelContext) } } actor MyModelActor: ModelActor { … init(container: ModelContainer) async { let context = (await ModelContext.makeBackgroundContext(for: container)).modelContext } … }
Nov ’23
Reply to SwiftUI List with Selection is Highlighting even in non-edit mode
I have the same use case where I only want rows to be selectable in edit mode. I solved this by checking whether edit mode was active before passing the selection binding to the List. For example: struct ContentView: View { let items: [Item] @Environment(\.editMode) private var editMode @State private var selectedItemIDs = Set<Item.ID>() var body: some View { NavigationStack { List(items, selection: isEditModeActive ? $selectedItemIDs : nil) { item in ItemView(item: item) } .toolbar { ToolbarItem(placement: .topBarTrailing) { EditButton() } } } } private var isEditModeActive: Bool { return editMode?.wrappedValue == .active } } This approach works. However, be aware that it does currently trigger a SwiftUI bug where the List no longer shows its multi-select checkmarks in edit mode. I reported the bug in FB13434460 (Open Radar ID 5544045669515264).
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’23
Reply to Will Private Relay impact on iOS App also
What about web views? WKWebView? SFSafariViewController?
Replies
Boosts
Views
Activity
Jul ’21
Reply to SwiftData does not work on a background Task even inside a custom ModelActor.
The bug is in SwiftData’s ModelContext initializer implementation. The initializer checks whether it is running on the main dispatch queue and if so, configures the context as a main context rather than a background context. More details in FB13399899 (Open Radar ID 5518888167014400). Here is my workaround: extension ModelContext { struct UncheckedSendableWrapper: @unchecked Sendable { let modelContext: ModelContext } /// Creates a background context. /// /// - Remark: This method works around FB13399899, which causes `init(_:)` to sometimes configure the instance /// as a main context. This method is marked `async` and not `@MainActor`, which guarantees that the method will be /// called off of the main thread, working around the bug in the initializer implementation. static func makeBackgroundContext(for container: ModelContainer) async -> UncheckedSendableWrapper { let modelContext = ModelContext(container) return UncheckedSendableWrapper(modelContext: modelContext) } } actor MyModelActor: ModelActor { … init(container: ModelContainer) async { let context = (await ModelContext.makeBackgroundContext(for: container)).modelContext } … }
Replies
Boosts
Views
Activity
Nov ’23
Reply to WeatherKit: Couldn't lookup symbols
Same for MusicKit.
Replies
Boosts
Views
Activity
Nov ’23
Reply to SwiftUI List with Selection is Highlighting even in non-edit mode
I have the same use case where I only want rows to be selectable in edit mode. I solved this by checking whether edit mode was active before passing the selection binding to the List. For example: struct ContentView: View { let items: [Item] @Environment(\.editMode) private var editMode @State private var selectedItemIDs = Set<Item.ID>() var body: some View { NavigationStack { List(items, selection: isEditModeActive ? $selectedItemIDs : nil) { item in ItemView(item: item) } .toolbar { ToolbarItem(placement: .topBarTrailing) { EditButton() } } } } private var isEditModeActive: Bool { return editMode?.wrappedValue == .active } } This approach works. However, be aware that it does currently trigger a SwiftUI bug where the List no longer shows its multi-select checkmarks in edit mode. I reported the bug in FB13434460 (Open Radar ID 5544045669515264).
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’23