Sharing a ModelContainer Instance Between App and WidgetBundle Targets in swiftui

I am encountering an issue when attempting to share the same ModelContainer instance between two separate targets: App and WidgetBundle. To achieve this, I created a StorageController to manage the ModelContainer and injected it into both targets.

// StorageController.swift
@MainActor
public static var globalInstance: StorageController = StorageController()

let modelContainer: ModelContainer
let modelContext: ModelContext

@MainActor
init() {
  do {
    let schema = Schema([
      Item.self,
    ])
    let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
    modelContainer = try ModelContainer(for: schema, configurations: config)
    modelContext = modelContainer.mainContext
    Logger().info("init modelContainer")

    guard let urlApp = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).last else { return }
    let url = urlApp.appendingPathComponent("default.store")
    if FileManager.default.fileExists(atPath: url.path) {
      Logger().info("swiftdata db at (url.absoluteString)")
    } else {
      Logger().warning("can't find swiftdata db")
    }
  } catch {
    fatalError("Could not initialize ModelContainer")
  }
}

// MyWidgetExtentionBundle.swift
@main
struct MyWidgetExtentionBundle: WidgetBundle {
  @MainActor
  init() {
    Logger().info("MyWidgetExtentionBundle init")
    let sc = StorageController.globalInstance

    AppDependencyManager.shared.add(dependency: sc)
  }

  var body: some Widget {
    MyWidgetExtention()
  }
}

// AppIntentsInWidgetExtentionApp.swift
@main
struct AppIntentsInWidgetExtentionApp: App {
  @State
  private var storageController: StorageController

  init() {
    let sc = StorageController.globalInstance
    storageController = sc
  }

  var body: some Scene {
    WindowGroup {
      VStack {
        Text("Total item cnt:(storageController.fetchItems().count)")
        Button("Add Item") {
          storageController.addItem(name: "Item-(UUID().uuidString)")
        }
      }
    }
  }
}

However, after adding logging to verify the location of the ModelContainer store file, I found that two different default.store files are being created.

# start with App
swiftdata db at file:///Users/username/Library/Developer/CoreSimulator/Devices/99106351-788D-4605-A5E6-475AD73ED05C/data/Library/Application%20Support/default.store
# start with WidgetBundle
swiftdata db at file:///Users/username/Library/Developer/CoreSimulator/Devices/99106351-788D-4605-A5E6-475AD73ED05C/data/Containers/Data/Application/7465C92B-AC5A-4AB0-BA68-2EEB78C16A38/Library/Application%20Support/default.store

What is the correct approach to ensure that the ModelContainer is shared between the App and WidgetBundle targets, so that data remains synchronized?

It turns out that the system treats the App and WidgetBundle as separate processes, preventing them from sharing containers. To enable container sharing across targets, follow the guide on using the App Groups feature.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật