After building out a sample project, it appears some derived data stuff hit me. Building the sample app proved that all the approaches above (including what I was doing initially) work. I cleared all my Xcode cache and tried it in my project and it seemed to work. Part of my issue as to why I wasn't seeing the data either might have been that this query
@Query(
filter: #Predicate { recipe in
recipe.book?.title == "Sample Book #1"
},
sort: \.name
)
private var recipes: [Recipe]
doesn't work. I'm not exactly sure why this is, maybe it's just me not fully understanding how #Predicate functions, but looking at it, this should work.
The query example that @malc posted doesn't appear to work either. I have prints in place to depict this. There are 3 different versions. 2 of them return an empty array, while 1 of them doesn't even compile. Now that i'm onto Queries, this is probably out of scope of this thread, but wanted to bring it up anyways in case anyone has any ideas there.
I've attached a sample project that explains the different approaches I have used for generating Sample data, as well as how I got around the @MainActor but with the #Preview macro. I figured this would probably be a good point of reference for anyone that ends up in my shoes.
I wasn't sure of the best way to handle uploading the sample project, so I threw it in my S3 bucket. http://anth.io/SwiftDataDemo.zip
Post
Replies
Boosts
Views
Activity
@610 That was my mistake. I was sort of trying to try other things in this project. Locally, Book.self is part of the schema. I'll put together a sample project will full source code that you'll be able to check out.
I quickly gave it a test and no dice. I'll put together a sample app and verify this. I can share that here as well as filing feedback for it. This part definitely feels broken.
The query suggestions from @malc did not work for me as well either, at least not with the PreviewContainer.
I'll get a sample app put together tonight and report back any findings I have 👍
If anyone does have any other suggestions, let me know
Yeah, I had a feeling I was going to have to do some weird stuff for queries like that. Not surprised given it's a Property Wrapper again in terms of fetching the data.
The issue I am seeing is that when creating the Recipe sample data, I'm getting a crash and the previews don't even load. I'm thinking this has to do with InMemory issues, but im not entirely sure.
I should have added this error to my initial post last night, but it was late, so I forgot.
`NSInvalidArgumentException`. Reason: Illegal attempt to establish a relationship 'book.