Issue with SwiftData in Sharing Data Across SwiftUI Project and Custom Keyboard Extension

I'm currently grappling with an issue while incorporating SwiftData into my SwiftUI project, specifically when dealing with a custom keyboard extension. The challenge lies in ensuring that the data I save and import through SwiftData is effectively shared between my main project and the custom keyboard extension.

Despite my efforts, it seems like the data isn't being shared as intended. I've reviewed my SwiftData implementation and the interactions between the main project and the extension but haven't been able to pinpoint the root cause.

      var body: some Scene {
        WindowGroup {
            TabView{
                ScreenOneView()
                    .tabItem {
                        Label("Main", systemImage: "square.and.pencil")
                    }
                Text("Setting")
                    .tabItem {
                        Label("Setting", systemImage: "square.and.pencil")
                    }
            }
        }
        .modelContainer(for: CopiedData.self)
    }
}

SwiftDataManager:

    class SwiftDataManager {
    
    static let shared = SwiftDataManager()

    func addItem(context: ModelContext, text: String){
      let item = CopiedData(text: text)
        context.insert(item)
    }
    
    func deleteItem(context: ModelContext, item: CopiedData){
        context.delete(item)
    }
}

View:

struct ScreenOneView: View {
    
    @Environment(\.modelContext) private var context
    @Query private var items: [CopiedData]
    
  List{
                    ForEach(items) { item in
                        HStack{
                            Text("\(item.text)")
                            Spacer()
                            buttons
                        
                     
                        }
                        
                    } .onDelete(perform: { indexSet in
                        for index in indexSet {
                            vm.delete(context: context, deleteItem: items[index])
                        }
                    })
                }

and the code from keyboard extension:

class KeyboardViewController: UIInputViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    setupView()

}

func setupView(){
    let keyboardView = UIHostingController(rootView: KeyboardView().modelContainer(for: CopiedData.self))  
    addChild(keyboardView)
    view.addSubview(keyboardView.view)

    keyboardView.view.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        keyboardView.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        keyboardView.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        keyboardView.view.topAnchor.constraint(equalTo: view.topAnchor),
        keyboardView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ])

    keyboardView.didMove(toParent: self)
}

keyboardView:

struct KeyboardView: View {
    
    @Environment(\.modelContext) private var context
    @Query private var items: [CopiedData]
    
    List{
                            
                            ForEach(items) { item in
                                contextTextCopied(text: "\(item.text)")
                                    .padding(.vertical, -6)
                            }
                            .onDelete(perform: { indexSet in
                                
                            })
                            
                            .listRowBackground(Color.theme.background)
                        }

to add and delete data i use only single instance

let swiftDataManager = SwiftDataManager.shared

Any updates on this? I’m facing the same issue to get the shared modelContext data in my Keyboard extension

Do I need to setup/config anything in the info.plist?

Issue with SwiftData in Sharing Data Across SwiftUI Project and Custom Keyboard Extension
 
 
Q