Can you expand on the "Write a predicate to fetch data" (to avoid the @Query on the main thread) My app uses a LOT of @Querys and the main thread is bogged down! I need more speed. Thanks!
So you can use something like this www.hackingwithswift.com/quick-start/swiftdata/how-to-create-a-custom-fetchdescriptor The only downside is that you'd have to manage refreshing the view yourself etc which the query macro does for you automatically.
great video, as always. +1 on a video on instruments!! I have a swift data project that is really bogging down when initiating a view that includes a dynamic filter predicate. I have no idea how to troubleshoot. thanks for what you do
When you set "Strict Concurrency Check" to "Complete", you will get a warning the message "Non-sendable type returned by call to actor-isolated function cannot cross actor boundary", How can void this.
You need to make your model sendable manually by marking it with the @unchecked Sendable pretty good article explaining it here but it's safe to use this since we don't modify the model across different threads.Here's a simple example below. @Model class MyModel: @unchecked Sendable { }
You have to use an actor so you can move it off the main thread and isolate it onto it's own thread which is what I explain in the video around 4:20, the ModelActor property wrapper lets you create a context away for the main one
@@tundsdev Hmm. In my test this also does move the operation to a background thread. I haven't done much with actors yet, so maybe actors are the preferred way to do it anyway. Here is the code the way I mean it: Task(priority: .background) { let newModelContext = ModelContext(TestApp.sharedModelContainer) (0...1000).forEach { index in let newItem = TestItem(uuid: UUID()) newModelContext.insert(newItem) } try? newModelContext.save() }
Nice video as always and yes... tutorial about Profile in Instruments!
You got it!
Thank you 🙌 do you have any videos on how to query it not on the main thread like you mentioned at the end?
Yes! A video 2:50 with that tool with be great.
I got you!!!
Can you expand on the "Write a predicate to fetch data" (to avoid the @Query on the main thread)
My app uses a LOT of @Querys and the main thread is bogged down!
I need more speed.
Thanks!
So you can use something like this www.hackingwithswift.com/quick-start/swiftdata/how-to-create-a-custom-fetchdescriptor
The only downside is that you'd have to manage refreshing the view yourself etc which the query macro does for you automatically.
@@tundsdev so what do you recommend? I’m only a part-time Programmer/hobbyist.
I would prefer something simplistic.
Awesome video, thanks! YES! I'd really love to see a tutorial about instruments
You got it!
Excellent video on ModelActor macro. It was very helpful.
Glad it was helpful!
great video, as always. +1 on a video on instruments!! I have a swift data project that is really bogging down when initiating a view that includes a dynamic filter predicate. I have no idea how to troubleshoot. thanks for what you do
Sounds great!
Helpful. Thank you sir !
Thanx for amazing video, but is it working also when i want to update data ?
Yes instruments tutorial!
Got it!
When you set "Strict Concurrency Check" to "Complete", you will get a warning the message "Non-sendable type returned by call to actor-isolated function cannot cross actor boundary", How can void this.
You need to make your model sendable manually by marking it with the @unchecked Sendable pretty good article explaining it here but it's safe to use this since we don't modify the model across different threads.Here's a simple example below.
@Model
class MyModel: @unchecked Sendable {
}
@@tundsdev Thanks for the explanation.
You can fetch using the ModelActor and then stuff a struct with the results and pass that as the struct is Sendable.
@@tundsdev Why is it safe to do this? Aren't SwiftData models thread-unsafe?
Why create an actor who you just reap everything into Task{let modelContext = ModelContext(container) ...} ?
You have to use an actor so you can move it off the main thread and isolate it onto it's own thread which is what I explain in the video around 4:20, the ModelActor property wrapper lets you create a context away for the main one
@@tundsdev Hmm. In my test this also does move the operation to a background thread. I haven't done much with actors yet, so maybe actors are the preferred way to do it anyway. Here is the code the way I mean it:
Task(priority: .background) {
let newModelContext = ModelContext(TestApp.sharedModelContainer)
(0...1000).forEach { index in
let newItem = TestItem(uuid: UUID())
newModelContext.insert(newItem)
}
try? newModelContext.save()
}