TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SwiftData outside of View

Forums > Swift


Ok, I get the whole idea of SwiftData is being tightly coupled to SwiftUI, but can it seriously not be decoupled AT ALL?!

I'm really struggling with the idea that I can't use SwiftData outside of a view, there are so many relevant scenarios that aren't in views. Some examples:

  • when a silent notification comes in, create and insert a model
  • search the datastore for a specific model
  • check the most recently updated model for more accurate refreshing
  • so many more reasons

So, has anyone found the proper way to access SwiftData outside of a view? Am I stupid?



I even tried doing this dumbness:

import SwiftData
import SwiftUI

struct SwiftDataInsertionView: View {
    @Environment(\.modelContext) var context
    var modelType: any PersistentModel

    init(modelType: any PersistentModel) {
        self.modelType = modelType
        let _ = insertToSD()

    var body: some View {

    func insertToSD() -> Bool {
        do {
            print("inserting to context")
        } catch {
            return false
        return true

It didn't work and crashed the app every time.


I've created an account just say that I feel the same and understand the pain of seeing this tight coupling. Maybe Apple's direction is to make things easy for beginners and reduce the complexity to develop simple apps. If you try to make an enterprise app with complex logic I feel you have to fight the frameworks, which is such a shame and usually a red flag. Anyways, here is one interesting attempt by Simon. He mentioned it here

I myself am looking also into a pragmatic solution for this problem. My hope is that more developers will join to find a way out. So far, I'm still using CoreData in my professional work with SwiftUI + MVVM + Clean Architecture.

Update: There is also a post regarding some separation But it's still coupled in the initializer with the context. But it's a step forward, although it's a shame to lose the Query property wrapper


How about using Core Data and Swift Data in the same app? Use Core Data for the methods outside a view.

See the (unfortuntely brief) topic "How to make Core Data and SwiftData coexist in the same app" in Paul's “Swift Data by Example” book.


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.