|
Having trouble implementing SwiftData on my working solution to day 60, Milestone: Projects 10-12. Specifically, I'm seeing an error on ContentView.swift, "Instance method 'insert' requires that '[User]' conform to 'PersistentModel'" Does anyone have any insight on what the issue could be? It seems I've wired up SwiftData correctly, and have all of the needed imports, but I'm having trouble debugging the issue. Also, i've tried to search the web and these forums for example solutions, but it seems that this is the new version of this challenge. From looking at past implementations, day 61 used to be a CoreData problem. AllMyFriends_ConsolidationVApp.swift
Friend.swift
User.swift
ContentView.swift
DetailView.swift
|
|
Also, I've tried to isolate what the issue is and see if I could hard code an object into the model and edited my ContentView.swift to the below. I'm getting weird results, including the Preview on Xcode constantly crashing and the NavigationLink routing being funky (needing to press back several times before getting back to homepage). So maybe something else is going wrong???
|
|
From the SwiftData tutorial, you can see that Paul inserts one instance of the object at a time
with the I encountered the same issue as you but solved it like this: What I did was to loop through the fetched results and the insert each model object. Just amend your loadData() method at this part.
|
|
Thank you, @swiftasroy ! That turned out to be the issue. I assumed that you could just pass the entire array of objects inside to the model. Turns out that you can only insert into Swift Data one object at a time. This doesn't seem super optimal and after doing some googling I found an article from Paul where he shows how you can batch insert objects into a Swift Data model in the background. Seems practical for larger data sets. |
SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.