|
How do I fetch the SwiftData into a class like I used to fetch CoreData into the class. Changes made to the data in the AppManager were saved.
That would be the model for the app. (Obviously this is very simplified test stuff).
Every example I've been able to find so far, fetches the data using @Query on a view. I would like the fetch the data into an array in a class. After reading the documentation, there is just one line of code to do the fetch. But how do you set the ModelContainer and the ModelContext in this class? |
|
To fetch data using SwiftData into a class, follow these steps:
|
|
hi Dennis, this is a little bit of quick guess, and it's not something i have used ... but say your AppManager needs a property to reference the SwiftData modelContainer:
when you begin your App, instantiate your AppManager; and then use the so something like this
that could answer your main question, and you can certainly then your AppManager can execute fetch calls on demand using the mainContext of the modelContainer. however, if you're thinking that you'd like to have an equivalent of Core Data's hope that's of some interest, DMG (i am not an AI) added ater the post: there's probably a more direct way to do this, where you create the modelContainer yourself in advance of having to rely on the |
|
How do we report a post as being possible spam/chatGPT crap? Reading the last post reads like a chatGPT post and is full of poor/misleading information. |
|
@weo asks
Send an email to Hint: Do not distract him with conversation, observations, questions, or improvement requests. STAY ON TOPIC. EMail SubjectThe email Subj: CHAT GPT SPAM in Forum Message LinkNext Look at the suspected spam message. (Look at your message, too.) Do you SEE the chain link icon? This is a direct link to the offending message. Click the FOR EXAMPLE: This is a LINK to your message. https://www.hackingwithswift.com/forums/swift/swiftdata-using-it-in-a-class/23803/24685 Email MessageYour ------ SAMPLE MESSAGE ----------- Love and Kisses, Follow UpNext: More than likely he will NOT acknowledge your message directly. Instead, he'll nuke the offending message! Ask Siri to set a reminder for you, incase you are the forgetful type. Keep Coding |
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.