# SwiftData context is empty after saving

**URL:** <https://forums.swift.org/t/swiftdata-context-is-empty-after-saving/75091>\
**Category:** Using Swift\
**Tags:** swiftdata\
**Created:** [October 1, 2024, 11:34pm UTC](https://forums.swift.org/t/swiftdata-context-is-empty-after-saving/75091 "2024-10-01T23:34:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielC](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@DanielC](https://forums.swift.org/u/DanielC)\
**Post date:** [October 1, 2024, 11:34pm UTC](https://forums.swift.org/t/swiftdata-context-is-empty-after-saving/75091/1 "2024-10-01T23:34:02Z")

</div>

Hi there,

Super beginner in Swift and SwiftData here. I created a simple project to import data from a .json file into a Swift model using SwiftData. The process involves loading and decoding the .json file, then saving the data into a model context. However, when I attempt to fetch the data from context, the result is empty. I’ve highlighted the issue within the code.

```swift
struct ContentView: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var dataItems: [DataItem]
    var body: some View {
            Button("Import JSON to SwiftData") {
                importJSONToSwiftData(context: modelContext)
// some other views
// ....
}

func importJSONToSwiftData(context: ModelContext) {
        if let jsonData = loadJSON() {
            if let decodedData = decodeJSON(data: jsonData) {
                saveToSwiftData(decodedData: decodedData, context: context)
            }
        }
    }

func saveToSwiftData(decodedData: [MyData], context: ModelContext) {
    
    for item in decodedData {
        let newData = DataItem(field1: item.field1, field2: item.field2)
        // the newData is as expected
        context.insert(newData)
    }
    do {
            try context.save() // Persist the changes to SwiftData
            
// PROBLEM: try to fetch but get nothing
        } catch {
            print("Failed to save data to SwiftData: \(error)")
    }
}

struct MyData: Codable {
    let field1: String
    let field2: Int
}

@Model
class DataItem: Identifiable {
    
    var id: String
    var field1: String
    var field2: Int

    init(field1: String, field2: Int) {
        self.id = UUID().uuidString
        self.field1 = field1
        self.field2 = field2
    }
}

```

Thanks for any responds.

---

<div class="post-metadata">

**Author:** ![jfhak](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@jfhak](https://forums.swift.org/u/jfhak)\
**Post date:** [March 1, 2025, 8:32am UTC](https://forums.swift.org/t/swiftdata-context-is-empty-after-saving/75091/2 "2025-03-01T08:32:40Z")

</div>

I encountered the same issue. The problem was that Model was not registered in the `ModelContainer`. After adding the model to the ModelContainer, everything worked correctly.

```swift
struct ExampleAPP: App {
    var body: some Scene {
        let config = ModelConfiguration(isStoredInMemoryOnly: true)
        let container: ModelContainer = try! ModelContainer(for: xxxModel.self, configurations: config)

        WindowGroup {
            ContentView()
                .modelContainer(container)
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Destriarch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/destriarch/32/42490_2.png) [@Destriarch](https://forums.swift.org/u/Destriarch)\
**Post date:** [August 13, 2025, 9:23am UTC](https://forums.swift.org/t/swiftdata-context-is-empty-after-saving/75091/3 "2025-08-13T09:23:37Z")

</div>

Nice one!

In my case, the reason the missing items weren’t in the model container was that my project contains two different targets, one for iPhone and one for Mac, and I’d neglected to change the one in the Mac app.

There really ought to be an error thrown when you try and insert an unsupported object into a database.
