SwiftData SortDescriptor Limitation...

  • I built a SwiftData App that relies on CloudKit to synchronize data across devices.
  • That means all model relationships must be expressed as Optional.
  • That’s fine, but there is a limitation in using Optional’s in SwiftData SortDescriptors (Crashes App)
  • That means I can’t apply a SortDescriptor to ModelA using some property value in ModelB (even if ModelB must exist)
  • I tried using a computed property in ModelA that referred to the property in ModelB, BUT THIS DOESN”T WORK EITHER!

Am I stuck storing redundant data In ModelA just to sort ModelA as I would like???

Not entirely sure what your issue is here. You say something crashes, but you don't explain what the crash is, so it's not possible to see whether you're doing something wrong or if there is a genuine issue in Apple's code.

You haven't provided any code whatsoever, so I really can't give you any help at all.

Can you provide some code that explains your issue properly?

I am not asking for help fixing a crash. I did that by commenting out one line of code where I was asking SwiftData to sort ModelA results on a title string from an associated ModelB object. ModeB has a one to many association to ModelA and must exist before any ModelA objects are created BUT CloudKit requires that you specify the relationships between these two models as Optional. Therefore when I tried to sort ModelA results by a Title in ModelB I needed to declare the property as a force unwrapped title of ModelB in the SortDescriptor. It would be ludicrous to try to show the actual code so that you can follow everything.

Suffice it to say that this SortDescriptor was accepted by the compiler, and gave me exactly what I wanted when I ran the App either in a Simulator or on a device that downloaded the app from Xcode. BUT when the app was uploaded to the App Store and then downloaded from TestFlight the App would immediately crash with no useful error pointing to the problem.

Bottom line is I found the issue after months of digging and now I just want to restore the capability I had before I had to comment out that line of code. As I said I tried using a computed property in ModelA (rather than store the title from ModelB it would fetch it when needed) and used that computed property as the sort descriptor but although this is also accepted by the compiler this fails as well.

I suppose I can do a sort on the array AFTER it is fetched by SwiftData but that seems less efficient. Anyway wondering if anyone else has run into this limitation and has found a better workaround.

This seems to the same issue reported here... Do you have a feedback report yet? If not, I’d suggest that you file a feedback report and share your report ID here.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Could be the same issue. I tried reporting this directly to Apple with a request for Technical Assistance because I hadn't seen any other reports of this behavior.

I had developed a SwiftData app that used CloudKit and therefore required all model relationships to be treated as optional.

I was sorting an object using a relationship's title field (which was clearly optional) and this worked fine in all simulator and Xcode downloads with no issues.

I didn't run into it until I tried using TestFlight to get some beta feedback and the app immediately crashed and the feedback was sparse so I took several months to finally track down what was causing it.

I would certainly like to see this fixed because it creates limitations that only surface very late ih the app dev process which is very frustrating. If it shouldn't ever work then it should also crash in the simulator and any Xcode downloads IMHO.

SwiftData SortDescriptor Limitation...
 
 
Q