Post not yet marked as solved
Okay, so I created a new Xcode project using SwiftData, and ticked the box for iCloud integration. I added a container on the web dashboard. Then I added “ = Date()” to the definition of Item.timestamp so that it has a default value. I added the container to my Xcode project..
Then I added this “let config” and “let container” and changed the “.modelContainer” like this:
This will successfully add stuff to core data, which also appears in iCloud dashboard.
BUT, if I delete the app and re-launch, I don’t see the existing iCloud data until the SECOND launch, even though the Xcode runtime log shows it fetched it.
Do I need to do some magic to get it to refresh? Is the CloudKit subscription managed by SwiftData or do I need to do something for that?
thanks!
Post not yet marked as solved
Where is the link to the Starter and completed projects for this session
Post not yet marked as solved
How do I customize my app's startup experience when using DocumentGroup in SwiftUI with SwiftData?
I don't want my users to be greeted with a document browser on startup.
Rather, I want to offer an experience like PhotoShop where the user is greeted by an "empty" version of my app's editor view. They can then use something like File->New or File->Open to get started.
I am getting this error in my preview:
CompileDylibError: Failed to build ContentView.swift
Compiling failed: main actor-isolated let 'previewContainer' can not be referenced from a non-isolated context
I don't know enough about @MainActor to figure this out.
Post not yet marked as solved
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?
Post not yet marked as solved
Failed to launch My Mac previews. Works fine for iPhone and iPad. Anyone having similar issues?
Hello! I have followed steps outlined in Build an app with SwiftData to create a document-based app to create and save a set of flashcards. I have defined by model as follows:
@Model
final class Card {
var createdAt: Date
var front: String
var back: String
init(front: String, back: String, createdAt: Date = .now) {
self.createdAt = createdAt
self.front = front
self.back = back
}
}
I am loading a document group as described in the video:
@main
struct DeckApp: App {
var body: some Scene {
#if os(iOS) || os(macOS)
DocumentGroup(editing: Card.self, contentType: .deck) {
ContentView()
}
#else
WindowGroup {
ContentView()
.modelContainer(for: Card.self)
}
#endif
}
}
Moreover, I have defined document and exported types in info.plist:
However, when I try to open a previously saved document, the items do not show up in the app, even though I can see some relevant strings in StoreContent-wal. Would anyone please be able to point me to something I must have missed? Thank you in advance.
Post not yet marked as solved
I made the changes described in the WWDC2023-10154 tutorial that were intended to allow the user to create a new (blank) document. However, every new document contains the "Compiler"/"Grace Hopper" card that was added to the persistent store in the earlier, non-document-based part of the video. I've tried a full clean and clearing derived data but I can't seem to get that card to go away.
What am I missing? How can I make sure that new documents are created without any data?
Hi all,
Has anyone stumbled upon the SwiftData equivalent of @SectionedFetchRequest? Is there a way to do it with @Query? I'll keep going through the documentation but if anyone has an answer, it would be much appreciated!!
Thank you.
I did manage to save my Entities to CloudKit with SwiftData but the default database is the private database. I need to store some Entities in the private and other Entities in the public CloudKit database. How do I manage that with SwiftData? With CoreData I always used different configurations for both private and public and added the entities to one or the other.
Post not yet marked as solved
Hi,
re: the SwiftData session "Create an app with SwifData" (https://developer.apple.com/videos/play/wwdc2023/10154)
I noted the mention of generating sample preview data. In CoreData, I had a similar need and this was achieved by creating a shared in-memory context; fairly straight forward.
The example given for SwiftData was decorated with @MainActor and then added to the target project, then in the #Preview closure, the modelContainer is amended to point to the preview data. Anyways, my problem is that I'm receiving an error when trying to render the preview in question:
main actor-isolated let 'previewContainer' can not be referenced from a non-isolated context
I suppose my issue is not having used the MainActor wrapper before and being unclear on what's possibly going wrong here (or if it's not just a "me" problem).
Can anyone help?
Post not yet marked as solved
With Core Data you can control the journal mode as follows:
let dictionaryOptions = [NSSQLitePragmasOption : ["journal_mode" : "DELETE"]]
Is there a SwiftData equivalent?
Post not yet marked as solved
Hi, does the build along require macOS Sonoma or can one simply use macOS 13.4 Ventura with Xcode 15 Beta?
Post not yet marked as solved
Hey there,
for my recent projects I used CoreData for persistence. One nice feature I use all the time is the ability to create a child context for editing operations.
When editing an entity in a form presented as a sheet, the changes made are only saved into the main context/parent context if the child context (I call this the editing context) is saved. Therefore dismissing an editing view results in destroying the child context and all the changes are discarded. Likewise when saving in the editing view, the changes will be proceeded to the parent context and persisted.
Is there any possibility to get the same or equivalent behavior when using SwiftData. Or is there another way to model this kind of cases?
All the best from Cologne, Germany!
Post not yet marked as solved
I am working on a SwiftUI application that needs to sync data across multiple devices using CloudKit. I am trying to decide between two options:
CKSyncEngine with a custom model
SwiftData plus CloudKit container.
This decision is crucial for me because the application I am developing is using tree structured data and the relationship between the data is very complex. I would appreciate your guidance on which option is better suited for my needs.
One of the benefits of using SwiftData is that it has a lot of interesting features that can save me a lot of development time, such as automatic migrations, batch operations, predicates, sorting, grouping, etc. However, one of the drawbacks is that it may not be flexible enough to handle the complex data display logic that I need for my application. For example, I need to show different views depending on the level and type of the tree nodes, and I also need to perform some calculations and transformations on the data before displaying it. I am afraid that using SwiftData will lead to more complexity and uncertainty when implementing these features, and there may be performance issues as well.
Another factor that I need to consider is the future support and development of these options. I believe SwiftData will have a good future because it is based on Apple's frameworks and technologies. However, I don't know if CKSyncEngine will be valued in the long run because it is a third-party framework that may not be updated or maintained regularly.
In conclusion, I am still unsure whether to use CKSyncEngine or SwiftData for my SwiftUI application that needs CloudKit sync. Both options have their pros and cons, and I need to weigh them carefully according to my requirements and preferences. I would love to hear your opinion on this matter.
Thank you for your time and attention.
Post not yet marked as solved
Did anyone successfully used transformable in SwiftData to store UIColor or SwiftUI Color type?
@Attribute(.transformable) var color: UIColor
re: the SwiftData session "Create an app with SwifData" (https://developer.apple.com/videos/play/wwdc2023/10154)
I corrected the @MainActor issue, but it seems to only work with the main ContentView and not other views.
I get the following error for TripListItem for example :
failed to find a currently active container for Trip
Post not yet marked as solved
I am trying to create a document-based (macOS) app using SwiftUI and SwiftData. I'm trying to set up XCTests to validate my model behaviors.
Clearly, I need to set up a container in my test class for my model objects. Failing to do so shows that SwiftData is trying: my app launches into the New Document dialog; if I dismiss it, the app fails with a "failed to find an active container" for my model object.
Any pointer on setting up a container and context within XCTest is welcome. TIA
Post not yet marked as solved
The 'unique' attribute is a really nice feature, BUT. In some of my apps, the unique identifier for an object is a combination of multiple attributes. (Example: a book title is not unique, but a combination of book title and author list is.)
How do I model this with SwiftData? I cannot use @Attribute(.unique) on either the title OR the author list, but I want SwiftData to provide the same "insert or update" logic.
Is this possible?
Post not yet marked as solved
I am trying to use SwiftData to perform a Query based on the name of the actor, which is inside a movie model. Here is the Movie model.
@Model
final class Movie {
var title: String
var year: Int
@Relationship(.noAction, inverse: \Actor.movies)
var actors: [Actor] = []
init(title: String, year: Int) {
self.title = title
self.year = year
}
}
Here is my actual Query:
_movies = Query(filter: #Predicate { $0.actors.contains(where: { $0.name.contains(actorName) }) })
But it returns nothing, even though I am passing actorName which exists in the movie.