Joe Sherwood
dev Photos About Archive Also on Micro.blog
  • ⌘ til • `SortDescriptor`s crash SwiftUI previews

    Sorting a SwiftData Query directly works on device and simulator. However use this in an init

    query = Query(filter: predicate, sort: [SortDescriptor(\Foo.bar?.baz])
    

    and the SwiftUI Preview canvas will crash. Previews seem to not have the ability to sort through an Optional chain and produce the correct sql command.

    Instead, a post-query, Array-based sort can be used.

    init() {
      query = Query(filter: predicate)
    }
    
    var body: some View {
      HStack() {
        ForEach(sorted) { qux in
          ...
        }
      }
    }
    
    private var sorted: [Foo] {
      query.sorted {
        ($0.bar?.baz ?? "") < ($1.bar?.baz ?? "")
      }
    }
    

    env • Xcode 26.5 • Swift 6.3 • iOS 26.5

    → 10:57 PM, May 31
  • ⌘ til • `SortDescriptor`s crash SwiftUI previews

    Sorting a SwiftData Query directly works on device and simulator. However use this in an init

    query = Query(filter: predicate, sort: [SortDescriptor(\Foo.bar?.baz])
    

    and the SwiftUI Preview canvas will crash. Previews seem to not have the ability to sort through an Optional chain and produce the correct sql command.

    Instead, a post-query, Array-based sort can be used.

    init() {
      query = Query(filter: predicate)
    }
    
    var body: some View {
      HStack() {
        ForEach(sorted) { qux in
          ...
        }
      }
    }
    
    private var sorted: [Foo] {
      query.sorted {
        ($0.bar?.baz ?? "") < ($1.bar?.baz ?? "")
      }
    }
    

    env • Xcode 26.5 • Swift 6.3 • iOS 26.5

    → 10:57 PM, May 31
  • ⌘ til • `SortDescriptor`s crash SwiftUI previews

    Sorting a SwiftData Query directly works on device and simulator. However use this in an init

    query = Query(filter: predicate, sort: [SortDescriptor(\Foo.bar?.baz])
    

    and the SwiftUI Preview canvas will crash. Previews seem to not have the ability to sort through an Optional chain and produce the correct sql command.

    Instead, a post-query, Array-based sort can be used.

    init() {
      query = Query(filter: predicate)
    }
    
    var body: some View {
      HStack() {
        ForEach(sorted) { qux in
          ...
        }
      }
    }
    
    private var sorted: [Foo] {
      query.sorted {
        ($0.bar?.baz ?? "") < ($1.bar?.baz ?? "")
      }
    }
    

    env • Xcode 26.5 • Swift 6.3 • iOS 26.5

    → 10:57 PM, May 31
  • RSS
  • JSON Feed