How can document settings/metadata be saved in SwiftData/SwiftUI?

Background: I had an old CoreData, document-based app where it was expected each document would apply to a specific year, in a specific state/province. It made the most sense to save the year and location on a per-document basis, outside of the CoreData model. I managed use the document’s metadata dictionary, accessed via the NSPersistentStore, as described in this question: How can document-based settings be stored?

In more detail, the code looked like this:

class Document: NSPersistentDocument {
    //other let/var here
    
    var persistentStore: NSPersistentStore? {
        if let stores = self.managedObjectContext?.persistentStoreCoordinator?.persistentStores {
            if stores != [] {
                return stores[0]
            }
        }
        return nil
    }
    
    @objc dynamic var documentYear = 2011 {
        didSet {
            setMetaData(documentYear as AnyObject, forKey: "year")
        }
    }
    
    @objc dynamic var documentProvince = 3 {
        didSet {
            setMetaData(documentProvince as AnyObject, forKey: "province")
        }
    }
    
    func setMetaData(_ object: AnyObject, forKey key: String) {
        if persistentStore != nil {
            var metadataDict = persistentStore!.metadata
            if metadataDict?[key] == nil || (metadataDict?[key] as! NSNumber) != (object as! NSNumber) {
                metadataDict?[key] = object
                persistentStore!.metadata = metadataDict
                updateChangeCount(.changeDone)
            }
        }
    }
    
    func getMetaData() {
        if persistentStore != nil {
            let metadataDict = persistentStore!.metadata
            if metadataDict?["year"] != nil {
                documentYear = metadataDict?["year"] as! Int
            }
            
            if metadataDict?["province"] != nil {
                documentProvince = metadataDict?["province"] as! Int
            }
        }
    }
    
    //other code here
}

I know it’s not exactly clean, but that’s not the point here…

Problem: Now updating to SwiftData, but trying to keep compatibility with my old files (even if I am basically the only user of this program).

I can’t find any documentation about an equivalent to NSPersistentStore in SwiftData/SwiftUI. This seems to mean I can’t get the metadata out of my old files (or create new document files with a SwiftUI version, using that previous method). The only mention of metadata in the SwiftData documentation seems to be on the persistent models themselves, which won’t help.

I can’t see how to subclass the persistent document in SwiftUI anyway, which means the previous method I used in the CoreData version wouldn’t work even if I did have an equivalent to NSPersistentStore. Additionally, if I can’t subclass the document, I don’t seem to have an alternative place to save document settings even outside the Persistent Store (if I were willing to break compatibility with old documents).

Question: Can document settings or metadata be used in SwiftData/SwiftUI, and if so, how? Is there a way to access the Persistent Store metadata that existed in my old CoreData files? Or is this something that just isn’t possible yet, pending this year’s WWDC, and my app’s migration to SwiftData will have to wait?

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