SwiftData App Crashes only in TestFlight...

I have been building a SwiftUI/SwiftData app for some time that has been working fine in both the simulator and devices I have loaded from Xcode. These include two iPhones an iPad and a VisionPro.

I have gotten to the point where I wanted to do some external beta testing so uploaded the App to the AppStore and then had a couple of users download via TestFlight. Unfortunately the App crashes and I have been unable to figure out why based on the feedback that I have gotten.

The app has 4 Models that are linked in a fairly straightforward manner. There is a Habit class that is the central object. An Aspiration class that may have several Habits associated with it. An Anchor Class and a Celebration Class both attach to a Habit.

I was able to get my neighbor to use TestFlight to download the App and it crashed when trying to insert a Habit but Aspirations, Anchors and Celebrations work fine and can be added, stored and deleted without issue.

I then had my neighbor connect their phone to my computer and I download the app directly and it works fine as expected even when not connected to the debugger so clearly something is different between the two environments and I am at a loss so could use someone with good knowledge to help me figure this out.

Answered by lrreynolds in 847098022

Well no thanks to Apple or anyone on here I did finally figure out what was causing the crash. It was something I had heard in passing in some Youtube video on SwiftData (I've watched so many I can't remember exactly where) I picked it up but it could well have been either Paul Hudson or Stewart Lynch said something about SwiftData not liking optionals in predicates.

Well it turns out that I was using a title of an optional object (although the way the app is implemented this Optional is really optional) in a sortDescriptor and this is what was causing the issue.

The super frustrating thing is it worked fine in both the simulator and locally downloaded from Xcode builds with not even a hint of a problem so the fact that it only showed up months into development when I finally decided to start external testing was unfortunate since by then the app was way more complicated and the errors reported via TestFlight were cryptic (Swifthdata Getter Exception?)

How is that supposed to help one figure out what is going wrong. Also after months of wrestling this this I finally tried reaching out to Apple Developer Support and that was a black hole. NOT IMPRESSED!

The only thing I can think of is that somehow you have a schema mismatch between the development and production versions of Cloudkit. Are you doing any Cloudkit syncing? Sorry I can't be of much more help.

Thanks for the suggestion. Yes I am doing CloudKit syncing of the entire SwiftData store that includes all four model types.

Not sure why if three of the models are working fine: to create, save, edit, delete why the fourth would crash the app but perhaps I am missing something more subtle.

Also I did not alter any settings for CloudKit for dev vs production I simply turned it on (in Signing and Capabilities) and assumed it was working for both dev and production environments. How should I check that?

UPDATE: Issue seems to be related to a view initializer and not to creating the Habit object. The view is part of the home view which is displayed immediately and this is what was causing the crash. I was able to get a version of the app to work with a simple ListView and no initializer and am now proceeding to add back the initializer one step at a time to find out what is the culprit. But again this issue never occurs in a build directly from Xcode so I need to add each new build to TestFlight to see if there is a problem. Wish I could get some Apple help on this...

Seems like it does not like any Predicate in the initializer. Even this simple initializer causes a crash (but not when I use the Xcode build).

    init(navigationPath: Binding<NavigationPath>, searchString: String = "", sortOrder: [SortDescriptor<Habit>] = []) {
        _habits = Query(filter: #Predicate { habit in
            if searchString.isEmpty {
                true
            } else {
                habit.title.localizedStandardContains(searchString) ||
                habit.detail.localizedStandardContains(searchString)
            }
        }, sort: sortOrder)
        self._navigationPath = navigationPath
    }
Accepted Answer

Well no thanks to Apple or anyone on here I did finally figure out what was causing the crash. It was something I had heard in passing in some Youtube video on SwiftData (I've watched so many I can't remember exactly where) I picked it up but it could well have been either Paul Hudson or Stewart Lynch said something about SwiftData not liking optionals in predicates.

Well it turns out that I was using a title of an optional object (although the way the app is implemented this Optional is really optional) in a sortDescriptor and this is what was causing the issue.

The super frustrating thing is it worked fine in both the simulator and locally downloaded from Xcode builds with not even a hint of a problem so the fact that it only showed up months into development when I finally decided to start external testing was unfortunate since by then the app was way more complicated and the errors reported via TestFlight were cryptic (Swifthdata Getter Exception?)

How is that supposed to help one figure out what is going wrong. Also after months of wrestling this this I finally tried reaching out to Apple Developer Support and that was a black hole. NOT IMPRESSED!

SwiftData App Crashes only in TestFlight...
 
 
Q