Meet SwiftData

RSS for tag

Discuss the WWDC23 Session Meet SwiftData

View Session

Posts under wwdc2023-10187 tag

28 Posts
Sort by:
Post not yet marked as solved
1 Replies
1.6k Views
Just for grins, I tried running the SwiftData generation tool on the existing Core Data model of a non-trivial app I've worked on for a decade or so. It's pretty substantial, and uses some more advanced features, so it seemed like an interesting test case. One of the warnings that was not reported by the UI, but which showed up in the generated code was quite a few instances of this: Entity inheritance on entity Bar (parent class Foo) is unsupported in SwiftData. Now that I'm looking at the code examples more carefully, I see an awful lot of final class, which maybe should have raised a red flag sooner. But to the best of my recollection, none of the sessions outright said that inheritance (was or) was not supported. Core Data has supported this functionality for a long time (maybe since the beginning). Assuming that this isn't supported in this first seed, are there plans to provide this functionality in the future? By the launch of iOS 17? (For the record, this model has 93 entities, of which 34 have parent entities. 14 are abstract, which I gather is also not supported. 3 of the entities are both abstract and have parent entities.)
Posted
by
610
Post marked as solved
1 Replies
1.5k Views
I just saw the available video according SwiftData and wanted to convert a project of mine. Both documentation and video mention this: By default, SwiftData includes all noncomputed properties of a class as long as they use compatible types. The framework supports primitive types such as Bool, Int, and String, as well as complex value types such as structures, enumerations, and other value types that conform to the Codable protocol. I tried to model an enum but I always get an error regarding the conformance to PersistentModel I made different type of enum all implementing RawRepresentable but I always get an error For example: enum Simple: Int16 { case one, two } @Model class Item { var simple: Simple = .one } I got those errors: No exact matches in call to instance method 'getValue' Candidate requires that 'Simple' conform to 'PersistentModel' (requirement specified as 'Value' : 'PersistentModel') Candidate requires that 'Simple' conform to 'Sequence' (requirement specified as 'Value' : 'Sequence') Did I miss something here?
Posted
by
Post not yet marked as solved
6 Replies
1.3k Views
I've been testing out SwiftData but haven't bee able to get ModelContext notifications working. I've tried both an objc observer and for await patterns but it never fires. If I listen for the older nsmanagedcontext notifications they are firing, but I am hoping that the new ones give an ID instead of an objectId. Has anyone got these working? Attempt 1: class NotificationObserver { init() { let didSaveNotification = ModelContext.didSave NotificationCenter.default.addObserver(self, selector: #selector(didSave(_:)), name: didSaveNotification, object: nil) } @objc func didSave(_ notification: Notification) { print(notification.name) } } Attempt 2: class NotificationObserver { init() { let didSaveNotification = ModelContext.didSave Task { for await note in NotificationCenter.default.notifications(named: didSaveNotification) { print(note) } } } }
Posted
by
Post not yet marked as solved
4 Replies
3.2k Views
Is SwiftData going to be immediately available to use with iOS 16? I ask because I'm working on an app which I'd like to release around the end of this month, and I'm about to implement CoreData for it, but wanted to see when SwiftData would be available. My guess was that SwiftData is for iOS 17 so I should just stick with CoreData for now and switch over later, but just wanted to check to make sure. Thanks!
Posted
by
Post not yet marked as solved
3 Replies
1.5k Views
What is the best way to use swift data to store one off data models? For example an application state that you want to be persisted across application launches. The only examples I've seen use arrays of items which wouldn't work for having just one application state. Is it possible to query just one item?
Posted
by
Post not yet marked as solved
1 Replies
779 Views
Hi, I could probably wait until tomorrow's deep dive session and just find out, but I'm curious to ask, are the SwiftData persistent containers still backed on SQLite primarily? I'm excited to replace Core Data in my existing SwiftUI app, but I've been dropping down to SQLite3 directly, for example, for using the FTS5 module for creating full-text-search tables alongside my Core Data tables to back more relevant results for .searchable UIs.
Posted
by
Post not yet marked as solved
3 Replies
1.2k Views
Hello, I have just tried playing around with the new Swift Data framework and noticed something. Assume the following many-to-many relationship on two PersistentModels: @Model class Media { @Relationship(.nullify, inverse: \Tag.medias) var tags: [Tag] } @Model class Tag { var medias: [Media] } This compiles without problems. If we now make Media conform to Codable (or just Decodable), we get a compiler error: "Ambiguous use of 'getValue(for:)'". When expanding the @Model and the then revealed @PersistedProperty macro, we see that the error is in the getter of Tag.medias, where we call self.getValue(for: \.medias). It seems the compiler knows multiple overloads of this function, including: an overload that accepts a KeyPath with a PersistentModel value an overload that accepts a KeyPath with a Decodable value Since Media conforms to both protocols, the compiler understandably does not know which overload to use. So to my questions: Is this intended behavior? So are PersistentModels not supposed to be Decodable? If yes, what would be the preferred way (or a clean way) to decode a PersistentModel (e.g., from an API)? Best regards, Jonas
Posted
by