Xcode Version 15.0 beta 5 (15A5209g), SwiftData Problem - "The given data was not valid JSON."

I have an app that uses Core Data and CloudKit that I am trying to convert into a SwiftData CloudKit app. Things were working very well until this most recent beta (beta 5) of Xcode 15.0. I have image data which the app saves as Data in external storage. I let Xcode convert my Core Data model in to a SwiftData model. As I said, things were working well. I could turn the image data into an Image for SwiftUI. That is, until beta 5. With beta 5, I get the following.

SwiftData/BackingData.swift:210: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON."

Well the data is NOT JSON, it is image data and yet, I get this fatal error. Does anybody else see this?

I have saved data using the app on the beta 5 simulator using SwiftData and I have verified that the data is correct using my core data app. The images are fine.

Answered by SpaceMan in 761239022

Looks like this has been fixed with Xcode 15 beta 6. Yay!

My app uses a CloudKit public database and a private database. The image data in the public database is saved as a CKAsset and the data from the downloaded asset is saved in SwiftData. That data is not JSON data. Again, this works well until beta 5.

I filed a feedback on this.

Also seeing an issue here. Glad to know that I am not the only one.

Exact same issue. Storing image data in external storage worked perfectly until Beta 5.

Same issue here, it seems it can not support Data type right now in Xcode 15 beta 5. Even empty Data() will through this error.

@Model
final class Item {
 
    var data:Data
    
    init() {
       self.data = Data() // when using it in a certain view, the same error happens
    }
}

I did find a workaround for this, simply wrap your Data object in a Codable struct, like this...

struct DrawingData: Codable {
    var data: Data
    init(data: Data) {
        self.data = data
    }
}

When you initialize the Model class, make sure to call an init with drawingData (or whatever variable type, as long as it's a codable struct) that is of type DrawingData (or whatever you rename this to).

Prior to wrapping the Data object, I had the same crash you all are reporting.

I also filed feedback :)

May I see your initializer?

Data is Codeable, but Data is not JSON. Again, one can code Data into JSON, but the Data is not JSON. So, the error message is bogus. I hope this issue has come to the attention of the Apple Developers.

Are there any Apple engineers out there that have seen this thread? Several of us filed a Feedback about this. I was just wondering if this is being brought to Apple's attention or whether we are whistling in the wind.

Accepted Answer

Looks like this has been fixed with Xcode 15 beta 6. Yay!

Xcode Version 15.0 beta 5 (15A5209g), SwiftData Problem - "The given data was not valid JSON."
 
 
Q