Post

Replies

Boosts

Views

Activity

Reply to Xcode 15 beta 7 Previews building issue
I have reported this issue using the feedback assistant at the time I created this thread. I am having trouble creating a minimal example project that reproduces this issue. Igor me, this issue only occurs in my production app. f anyone can provide me an example project, I will attach it to my feedback report For Apple to inspect.
Feb ’24
Reply to Looking for tutorial on how to use SwiftData correctly in background threads
I figured out a working solution, tested on iOS 17.3 on device. I will copy/paste code snippets from around this forum to compile a short how to. First, create a model actor, that is used to access swift data: @ModelActor actor MyModelActor { init(container: ModelContainer) { modelContainer = container let context = ModelContext(container) modelExecutor = DefaultSerialModelExecutor(modelContext: context) } // example usage func create() { let item = Item() modelContext.insert(item) try! modelContext.save() } } Snippet copied from https://developer.apple.com/forums/thread/736154 and modified for better readability. Second, initialize the model actor in a detachted task: Task.detached { let actor = MyModelActor() await actor.create() } Information from https://forums.developer.apple.com/forums/thread/736226 As of iOS 17.3 creating the model actor in a detached task is imperative because otherwise it will always execute on the main thread. This seems to be a bug in SwiftData that is covered extensively in the linked forum post. (this one was killing me) Thanks to @Fat Xu and @rkhamilton for pointing me into the right directions.
Feb ’24
Reply to searchable search bar loses input onSubmit or cancel
In case anyone is wondering how to fix this issue I describe what is happening and how I worked around it. When a user is submitting their input from a searchable view the searchable view will display an empty input field. However, the value bound to the view still has the value, the user submitted, it is just not displayed. This seems to be a bug in SwiftUI. I worked around it by first setting the bound value to an empty String, waiting a short period of time and the setting the value to the previous user input again. import SwiftUI struct SomeView: View { @State private var searchExpression = "" @State private var temporarySearchExpression = "" // a new state to temporarily hold the search expression @State private var isPresented = false init() { } var body: some View { NavigationStack { VStack { Text("some view") .searchable(text: $searchExpression, isPresented: $isPresented) .onSubmit(of: .search) { isPresented = false temporarySearchExpression = searchExpression searchExpression = "" // Waiting for a short period of time is necessary. // It seems SwiftUI needs some time to process the state changes. DispatchQueue.main.asyncAfter(deadline: .now().advanced(by: DispatchTimeInterval.milliseconds(1))) { searchExpression = temporarySearchExpression } } } } } } #Preview { SomeView() }
Aug ’23
Reply to SwiftData framework crashes application when enumerations are part of arrays
I can confirm this issue for Xcode Version 15.0 beta 4 (15A5195m). For me this issue also occurs for arrays of enums in a model. @Model final class Entity { var values: [SomeValue] init(values: [SomeValues]) { self.values = values // Fatal error: Illegal attempt to use a Optional(Swift.Mirror.DisplayStyle.enum) as a Property } } enum SomeValue { case value1 case value2 } ... let object = Entity(values: [.value1, .value2]) I have submitted a bug report: FB12674261
Jul ’23