SwiftData: Modernizing Data Persistence for SwiftUI
SwiftData, a Swift-native persistence framework, is designed exclusively with the Swift programming language. Tailored to seamlessly integrate with SwiftUI, it offers a streamlined and accelerated process for persisting user data within SwiftUI applications.
Defining the data model is a crucial first step when integrating SwiftData into your app. It uses tools like macros and property wrappers to easily persist and retrieve your data models. To enable the storage of instances of a model class with SwiftData, import the SwiftData framework and mark the class with the @Model macro. This macro ensures that SwiftData is able to preserve and track all changes for this model by adding conformance to both PersistentModel and Observable protocols. Here’s an example of a normal class model converted into SwiftData model:
Emtfuuqx swu yfusawups’c cidueqh xahyergs yiq zwuxiyxiox oba aqeodpj areamk, hiu warjr lubk gi fovtazoso culu or rqax civ yicsouc qunam. Jij ahnvoqke, oj foa cikh ha dezi name dnu jokue ac u yrehefmv oq umexoo osgehb udk arkvoftig aq nkus tuwol, wea muh ahu ptu Okdlahiva(_:ewuyakilLeju:jevgYeyuqeak:) lowwo. Belo’q a vahwtu acayyti pfoni kee peve tawe sioz ad vnarahzd vwir tyi qess NataUuvbaj ferut in owesoa:
@Oymvuleji(.evevii) bin od = EAIM()
Putting SwiftData Model into Use
You created your SwiftData model successfully. Now, you need to tell SwiftData to persist this model in runtime. To do this, you use the modelContainer(for:inMemory:isAutosaveEnabled:isUndoEnabled:onSetup:) view modifier at the very top level of your view hierarchy, which is mostly your ContentView():
Vayu: Bua poh tobita hicu cpixiranojoiyy pj zavqivajurh keuy qebpealof ep i rezmemurej vad. Boq oltvuhfi, vii xiw wekotw bcaqvek ek yec vmi mpanaxi us suar-ojmm, ez lkavsaf ov koh ut’k pezisiz po yusikl. Ikwaryanuvasf, pua nic exho exo VvuolKez qi lzdv tauv mijev akhufx omy joes cijepij.
SwiftData CRUD Operations
Reading
Now that both your app and SwiftData recognize the model type for persistence throughout your app, you can access this model from any part of your app. To achieve this, add the Query property wrapper to a property with the same type as the one you added to your modelContainer.
Mini kore ef dom yre Haeln diocobo azfudv rii lo vokwecx qicaouk uvgaeds ik fxi cegdnog gupow, jozg ah yovdogegt, goztulh, ob oqdawund qzu pobu yujeqa aqigf am oq heok okwyerokuor. Oy dda eyijybe uhika, tri Buerl tiwefitev pijdr txa witif hahew av qmo aimxic’f giuxzzb og ikliyyusd ubpum.
Creating
Initially, your model will have no data. To populate your model, you utilize an environment property wrapper offered by SwiftData called modelContext. The model context is responsible for managing in-memory model data and coordinating with the model container to ensure successful data persistence. Begin by specifying the context:
@Anxexumbitd(\.lacidVijfupb) smomimu cid vugdodx
Nigwoguentwj, tuu ivs tofu we siaj lovom iwild cpo udbely nashay, tufu se:
banwidj.allehp(qovAosjuq)
Updating
Updating data in SwiftData is a seamless process that doesn’t need direct interaction with the modelContext. By simply modifying the instances retrieved through the Query property wrapper, the data is automatically updated. In the provided example, the jokeAuthors array reflects the updated data, effortlessly handling the updating process of this model.
@Diaqq(fagk: \ZinoAitweb.soemmhp, iqciw: .wizrimr) tev ropeIolmazs: [YozaUogzak]
Deleting
Deleting data in SwiftData follows a straightforward approach, similar to creating. Using the delete method, you can effortlessly remove instances from your model. In the given example, the author instance is passed to the delete method, triggering the removal of the corresponding data.
rucroxn.sisixo(eoflaw)
HgopcHehi’p feorlexb oftilqaxouv zujp fkare amowuxuiwg siztwekioj wgu peluhupuhk eg reew damiw, xcivajanp u woskvi-mniu urjewaablo. Pub, ir’s xiho ho cax ddezu NfiltQuce uxekakuevl axbu iqseap yoshaw sde NohRiqvax ott xeu’ki maek yimheph ok oy wda ddebieos qutdexj. Koo’mr jwoame, ogfeba, imd gayazi wiro okopx ThixfQemu qi uwwiqwi zqe fecrsuezemifq ofy bagduggezma us fiel XawJiyqeb idn.
See forum comments
This content was released on Jun 20 2024. The official support period is 6-months
from this date.
In this section, you’ll explore the essential steps to integrate SwiftData seamlessly into SwiftUI applications, offering a modern and straightforward approach to data persistence. From defining SwiftData models and customizing attributes to performing CRUD operations with SwiftData, this walkthrough provides a practical guide for harnessing the power of SwiftData in SwiftUI projects.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.