Post

Replies

Boosts

Views

Activity

Reply to SwiftData: filtering against an array of PersistentIdentifiers
I don’t think Apple stores the persistent identifier in the database, only just the primary key. One possible reason why primitive types only work with arrays would be because the store their working with (SQLite) only support those types. Custom structs can be stored, but IIRC you can only query them if they are stored as JSON strings. But if you’re just passing an array it is just mapping those bindings that were laid out in the SQL/predicate. I did notice that when you use model(for:) it creates a predicate for persistent identifiers as a Set, so they probably have a switch of primitive collection types supported and some for other internal use cases. You could just use a unique ID string from your model and pass the entity’s name.
Jun ’25
Reply to Previews due to SwiftData Predicates in Xcode 16.3
So I wanted to confirm if this crashes with protocols too and it does when I access its id despite it being a stored attribute. Inserting, then saving, and tapping on fetch results in a crash with the same errors. import SwiftData import SwiftUI #Preview { ContentView().modelContainer(for: Media.self, inMemory: true) } struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var models: [Media] var body: some View { VStack { Button("Fetch") { if let model: Media = try? modelContext.fetch(FetchDescriptor(predicate: predicate(id: "test"))).first { print("model: \(model.id)") } } Button("Models: \(models.count)") { let media = Media( id: models.contains { $0.id != "test" } ? "test" : UUID().uuidString, type: .image ) modelContext.insert(media) try? modelContext.save() } ForEach(models) { Text($0.id) } } } } func predicate<T>(id: String) -> (Predicate<T>) where T: ID { let predicate = #Predicate<T> { media in media.id == id } return predicate } protocol ID: Identifiable where ID == String { var id: Self.ID { get } } @Model class Media: ID, Identifiable { @Attribute(.unique) var id: String @Attribute var type: MediaType init(id: String, type: MediaType) { self.id = id self.type = type } } struct MediaType: Codable, Equatable, Hashable { static let image: MediaType = .init(value: 0) static let video: MediaType = .init(value: 1) static let audio: MediaType = .init(value: 2) var value: Int }
Mar ’25
Reply to Previews due to SwiftData Predicates in Xcode 16.3
The crash occurs with just #Preview { ContentView().modelContainer(for:) } when I don't use traits, but I normally use traits, here's one I use: struct SampleData: PreviewModifier { static func makeSharedContext() throws -> ModelContainer { let schema = Database.schema let configuration = ModelConfiguration(isStoredInMemoryOnly: true) let modelContainer = try ModelContainer(for: schema, configurations: configuration) return modelContainer } func body(content: Content, context: ModelContainer) -> some View { content.modelContext(context.mainContext) } } I also use this in my traits: func body(content: Content, context: ModelContainer) -> some View { content.modelContainer(context) } When I removed predicates that accesses a struct's sub property or fetching model in a generic function that lets me access id, then it stops crashing any of my previews with/without traits, I can use SwiftData without issues. I decided to create a separate project and ran this code below, which was enough to crash previews for my Mac Studio (black screen and a red x mark). I'm noticing now that it doesn't crash if I remove .modelContainer(_:), but I won't be able to use SwiftData. import SwiftData import SwiftUI #Preview { ContentView().modelContainer(for: Media.self) } struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query(filter: Media.predicate) private var models: [Media] var body: some View { VStack { Button("Models: \(models.count)") { let media = Media(type: .init(value: .random(in: 0..<3))) modelContext.insert(media) try? modelContext.save() } ForEach(models) { Text($0.id.uuidString) } } } } @Model class Media: Identifiable { @Attribute(.unique) var id: UUID = UUID() @Attribute var type: MediaType init(type: MediaType) { self.type = type } static var predicate: (Predicate<Media>) { let image = MediaType.image.value let predicate = #Predicate<Media> { media in media.type.value == image } return predicate } } struct MediaType: Codable, Equatable, Hashable { static let image: MediaType = .init(value: 0) static let video: MediaType = .init(value: 1) static let audio: MediaType = .init(value: 2) var value: Int } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } } I'm gonna see if my iPad does the same... Also, I mentioned this in my updated bug report, but it turns out I always had this issue since Xcode 16, my preview constantly crashed and using legacy mode fix it. I was able to pin point the cause with Xcode 16.3 (it mentions Fatal Error in Schema.swift in 16.3, instead of Fatal Error in DataUtilities.swift before).
Mar ’25
Reply to Issues with NSBundle failed, VTParavirtualization, IOServiceMatchingFailed, AppleM2ScalerParavirtDriver
Yes, I am using Xcode 16 beta 3 on macOS Sequoia beta 3 on both host and guest in the VM. The issue is present when I ran my app in UTM or Apple’s virtualization sample project. I see this issue appear when I run my app, specially whenever it creates thumbnails. It happens when I use writeHEIFRepresentation and writeJPEGRepresentation, and I’ve tried other ways like ImageIO and AVFoundation which fails the same way. I recently discovered that setting useSoftwareRenderer to true for CIContext at least let me write JPEG images but not HEIF.
Jul ’24