# Repeated SwiftData error in finding currently active container

**URL:** <https://forums.swift.org/t/repeated-swiftdata-error-in-finding-currently-active-container/68545>\
**Category:** Using Swift\
**Tags:** swiftdata\
**Created:** [November 20, 2023, 6:12am UTC](https://forums.swift.org/t/repeated-swiftdata-error-in-finding-currently-active-container/68545 "2023-11-20T06:12:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterSteiner](https://avatars.discourse-cdn.com/v4/letter/p/50afbb/32.png) [@PeterSteiner](https://forums.swift.org/u/PeterSteiner)\
**Post date:** [November 20, 2023, 6:12am UTC](https://forums.swift.org/t/repeated-swiftdata-error-in-finding-currently-active-container/68545/1 "2023-11-20T06:12:17Z")

</div>

Hello everybody, I am a German math student trying to build some super simple educational apps. I always run into the same problem:

Error message: Thread 1: Fatal error: failed to find a currently active container for Category

Occurs in the macro Code in the line::

```swift
@Transient
private var _$backingData: any SwiftData.BackingData<Category> = Category.createBackingData()

```

I injected the Container in the Main File:

```swift
@main
struct TodointimeApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }.modelContainer(for: Category.self)
    }
}

```

my Model is pretty easy:

```swift
import Foundation
import SwiftData

@Model
class Category {
    var name: String
    var tasks: [TaskModel] = [TaskModel]()
    
    init(name: String, tasks: [TaskModel]) {
        self.name = name
        self.tasks = tasks
    }
    
}

@Model
class TaskModel {
    var title: String
    var datetodo: Date
    var takentime: TimeInterval
    var isdone: Bool
    var category: Category
    
    init(title: String = "Default Task", datetodo: Date = .now, takentime: TimeInterval = 0, isdone: Bool = false, category: Category) {
        self.title = title
        self.datetodo = datetodo
        self.takentime = takentime
        self.isdone = isdone
        self.category = category
    }
}

```

This occurs in everything that I am trying to do and I just can't figure out what's wrong. I would be super grateful about some tips.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![MadeByDouglas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/madebydouglas/32/29588_2.png) [@MadeByDouglas](https://forums.swift.org/u/MadeByDouglas)\
**Post date:** [November 24, 2023, 2:02pm UTC](https://forums.swift.org/t/repeated-swiftdata-error-in-finding-currently-active-container/68545/2 "2023-11-24T14:02:56Z")

</div>

Did you check the cloudkit box by any chance? It may be looking for something there, I think cloudkit containers want all the vars to be optional

---

<div class="post-metadata">

**Author:** ![AppleScruff](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@AppleScruff](https://forums.swift.org/u/AppleScruff)\
**Post date:** [November 25, 2023, 8:48pm UTC](https://forums.swift.org/t/repeated-swiftdata-error-in-finding-currently-active-container/68545/3 "2023-11-25T20:48:57Z")

</div>

For your var tasks, try removing the () after [TaskModel] and initialize it as an empty array to start. That is what helped me remove this error. Not sure of another work around.

---

<div class="post-metadata">

**Author:** ![AnthonyBY](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonyby/32/38128_2.png) [@AnthonyBY](https://forums.swift.org/u/AnthonyBY)\
**Post date:** [January 21, 2025, 5:46am UTC](https://forums.swift.org/t/repeated-swiftdata-error-in-finding-currently-active-container/68545/4 "2025-01-21T05:46:37Z")

</div>

This happened to me on iOS 17.5 (it works fine on iOS 18). What I did was simply initialize the ModelContainer in the @main struct. The odd thing is that I don’t use this container reference anywhere, but it does fix the crash issue.

```swift
let container: ModelContainer?

init() {
   do {
       let config = ModelConfiguration(isStoredInMemoryOnly: true)
       container = try ModelContainer(for: Category.self, configurations: config)
   } catch {
       print("Failed to initialize ModelContainer: \(error)”)
       container = nil
   }
}

```
