SwiftData Basics in 15 minutes

SwiftData, announced at WWDC 2023, is a framework that lets developers use Swift-like API to handle persistence in their apps. It lets you save data to the local device and then to retrieve, update and delete that data!
👋 Our "Launch Your First App" program will help you build up your iOS skills to publish your very own feature rich app even if you’re a beginner starting from scratch. cwc.to/youtubeoffer
In this SwiftData basics video, you’ll learn:
How to set up their Xcode 15 project to use SwiftData
How to add data, retrieve data, update data and delete data using SwiftData
How SwiftData is different from the CoreData framework
There are 5 steps involved:
00:00 Introduction
01:24 Step 1: Create the model
04:24 Step 2: Create modelContainer
05:15 Step 3: Environment object for the context
06:08 Step 4: Add objects and Query to get the data
09:37 Step 5: Update objects, remove objects from or save the context
This video is based on our SwiftData article here:
codewithchris.com/swift-data/
To get access to all of our courses, visit our website:
codewithchris.com/plus

Пікірлер: 131

  • @CodeWithChris
    @CodeWithChrisАй бұрын

    👋 Our "Launch Your First App" program will help you build up your iOS skills to publish your very own feature rich app even if you’re a beginner starting from scratch. cwc.to/youtubeoffer

  • @sk4tec
    @sk4tec11 сағат бұрын

    Ahh - feels like I'm back home again witha CodeWithChris video!

  • @danuff
    @danuff8 ай бұрын

    Simple, and to the point. Thank you!

  • @BasicInsideOut
    @BasicInsideOut11 ай бұрын

    Really good and simple description, especially for the CRUD operations.

  • @divsyntax9720
    @divsyntax97204 күн бұрын

    Haahahhahahahahahah when u were searching for the icon, I CAN RELATE!!! 😂 Awesome tut btw!

  • @karlostj4683
    @karlostj46834 ай бұрын

    Great job! Thanks for the basics, which is what everyone should start with!

  • @andriybelizky9318
    @andriybelizky93186 ай бұрын

    Awesome Video, I appreciate simplicity of examples.

  • @virendragupta5945
    @virendragupta594510 ай бұрын

    Great Demo Chris ....Lot easier than earlier database access !!! ... Thanks :D

  • @warrdadlani8494
    @warrdadlani8494Ай бұрын

    Brilliant. Chris you're great.

  • @daxuan90
    @daxuan905 ай бұрын

    Your tutorial is clear and easy understand. Thank you! ❤

  • @user-tf9pt7yy7w
    @user-tf9pt7yy7wАй бұрын

    this is the best swiftdata tutorial!!! thank you!!

  • @CodeWithChris

    @CodeWithChris

    Ай бұрын

    Glad it was helpful! Thanks for watching! - Iñaki

  • @helena9210
    @helena921010 ай бұрын

    Thanks!!! Fast focused and clear

  • @CodeWithChris

    @CodeWithChris

    10 ай бұрын

    Thanks for learning with us! - Iñaki

  • @thetutorialdoctor
    @thetutorialdoctor6 ай бұрын

    Excellent short yet complete tutorial on how to create a swift ui CRUD app. Appreciate this.

  • @CodeWithChris

    @CodeWithChris

    6 ай бұрын

    Glad it was helpful! Thanks for learning with us! - Iñaki

  • @neila880
    @neila88011 ай бұрын

    Thanks for the video. SwiftData looks really nice!!

  • @yourbestsail
    @yourbestsail9 ай бұрын

    Excellent teaching skills 😊

  • @wanderer1357
    @wanderer135711 ай бұрын

    Great video. Much easier than Core Data!

  • @ameliasaomarcos
    @ameliasaomarcos8 ай бұрын

    I do it now, with your tutorial. thanks.

  • @tonyhawk123
    @tonyhawk12311 ай бұрын

    Nice clear presentation style. Cheers!

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Glad you liked it!

  • @kaato33
    @kaato334 ай бұрын

    Thank you! I was scared to approach this topic but you made it easy to understand!

  • @CodeWithChris

    @CodeWithChris

    3 ай бұрын

    Hello! We're glad you found your video valuable! And thank you for the appreciation. :) --Joash

  • @RiccardoAlfano317
    @RiccardoAlfano31711 ай бұрын

    Great video I just started with swif but I’m pretty secure that I’ll learn Alot with your KZread video Italia Grazie

  • @eman31683
    @eman3168311 ай бұрын

    Wow that is a lot easier than CoreData directly.. Good video Chris and thank you for your time!

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    You're welcome, glad you liked it!

  • @bobmcnerney1
    @bobmcnerney15 ай бұрын

    Very easy tutorial to follow. Thanks!!

  • @CodeWithChris

    @CodeWithChris

    5 ай бұрын

    Glad it was helpful! Thanks for watching! - Iñaki

  • @rapier64
    @rapier649 ай бұрын

    Thanks for this very good tutorial!!!🙂

  • @CodeWithChris

    @CodeWithChris

    9 ай бұрын

    Glad it was helpful! Thanks for watching! - Iñaki

  • @pnkbrn
    @pnkbrn11 ай бұрын

    Thanks! Would love to see a video on sorting and filtering Query data.

  • @benjaminfaershtein138
    @benjaminfaershtein13811 ай бұрын

    Hey I just started watching your videos and I was intrested your collection of keyboards that are always prominent in your videos. Would be cool to see a video talking about your keyboards or a setup tour.

  • @vincent2700
    @vincent270010 ай бұрын

    Thanks for the introduction on this

  • @CodeWithChris

    @CodeWithChris

    10 ай бұрын

    Thanks for learning with us! - Iñaki

  • @mario_luis_dev
    @mario_luis_dev11 ай бұрын

    great demo Chris, really liked the way you explained it. New sub here :)

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Awesome, thank you and happy learning! --Joash

  • @prems1331
    @prems133111 ай бұрын

    Wonderful Video. Love it ♥

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Thanks for watching! - Iñaki

  • @blackookami5621
    @blackookami562111 ай бұрын

    that was really cool. thx

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Glad you liked it!

  • @ameliasaomarcos
    @ameliasaomarcos9 ай бұрын

    Nice.!!!

  • @sirflimflam
    @sirflimflam11 ай бұрын

    I find it kinda funny I was asking "why the underscore on the parameter name?" as you kinda just glazed over it in the delete function and I HAD TO KNOW so I went and looked it up. Then a bit later you explain it for the update function.. I SHOULD HAVE TRUSTED YOU GOOD SIR. SwiftData is pretty awesome though. I'm...struggling to get a good grasp on Swift. I've been doing C and C# for much of my life and something about Swift just throws me off. But your channel has at least put me in the right path.

  • @anhalexdinh2190
    @anhalexdinh21904 ай бұрын

    very great tutorial for basic concept

  • @Hendrik6
    @Hendrik611 ай бұрын

    Nice explanation, thanks a lot 😊

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Glad you liked it

  • @fdurmaz
    @fdurmaz11 ай бұрын

    Thank you @CodeWithChris

  • @thompsonms9941
    @thompsonms99419 ай бұрын

    Good one bro , Thanks !

  • @CodeWithChris

    @CodeWithChris

    9 ай бұрын

    Thanks for watching, and thanks for learning with us! - Iñaki

  • @jangelGuerrero
    @jangelGuerrero9 ай бұрын

    Amazing, I love this example, I have all to use it, thanks you and success in everything you do, greeting from México City.

  • @CodeWithChris

    @CodeWithChris

    9 ай бұрын

    Glad it was helpful! Thanks for learning with us! - Iñaki

  • @antonreashin
    @antonreashin7 ай бұрын

    thanks, I'm just going to use this in my project🙏

  • @CodeWithChris

    @CodeWithChris

    7 ай бұрын

    Have fun! Thanks for watching! - Iñaki

  • @sabata657
    @sabata6572 ай бұрын

    so good video my bro

  • @user-it5bw6em3j
    @user-it5bw6em3j4 ай бұрын

    Excelente

  • @nil_at
    @nil_at10 ай бұрын

    Is there some kind of visual view integrated to Xcode to see the current database layout, entities, relations, and data and so on? Like pgAdmin for Postgres for example?

  • @michaellittle1665
    @michaellittle166511 ай бұрын

    Thank you Chris that is good, I'm going to be undertaking in my second semester as part of my masters in IT the unit in iPhone software engineering. I hope they are up-to-date as you are!

  • @The1tjc
    @The1tjc11 ай бұрын

    Fyi, this is not showing up in KZread under your videos, nor did it pop up in my feed. I discovered it from your twitter post.

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Our Twitter post was a bit... prematured! Oopsy! @AdrienVillez

  • @user-ju4dz7pu3q
    @user-ju4dz7pu3q8 ай бұрын

    Love Your videos!! I have started a project in SwiftData, but I can not get the @Query to filter on date?? I've checked all the forums, and many people seem to be struggling, but no real answers that work for me. Any advice on filtering on date in SwiftData??

  • @Pedro76mchlkg
    @Pedro76mchlkg5 ай бұрын

    Thank you very much. I have a question, if I add a variable to the model, will it break the model?

  • @chezchezchezchez
    @chezchezchezchez10 ай бұрын

    Brand new to Mac development. Never used coredata. Where can I find the rest of the tutorial that explains things like… how .onDelete creates these magical indexies , and why they swipe view shows up without any code, showing anything about swiping etc. thanks!

  • @ElantraMan
    @ElantraMan5 ай бұрын

    How would this work using the likes of a view model?

  • @geoffstokeld4015
    @geoffstokeld401511 ай бұрын

    Do you still need Identifiable protocol? Really good video thank you. From another tutorial ‘SwiftData automatically handles the identification aspect of the model, eliminating the need for explicitly defining an id property or conforming to the Identifiable protocol.’

  • @w0mblemania

    @w0mblemania

    11 ай бұрын

    I'd still add a UUID (not a String). That way, it allows you to separate out distinct models instances, even if they happen to share the same data. It would be better for your own sanity, and debugging.

  • @ho6y
    @ho6y8 ай бұрын

    Absolutely well explained, really simple compared to CoreData

  • @CodeWithChris

    @CodeWithChris

    7 ай бұрын

    Glad you like it! Thanks for watching! - Iñaki

  • @LebenWerden
    @LebenWerden25 күн бұрын

    Hi, how I can use onMove modifier on Query?

  • @penyt
    @penyt10 ай бұрын

    Just wondering, if we follow MVVM, can we put that @Environment context in the ViewModel class?

  • @penyt

    @penyt

    10 ай бұрын

    I've read up quite a few information since, I feel like with SwiftData MVVM seems less relevant and it might become more complicated if we strictly want to implement MVVM.

  • @ebsm93
    @ebsm939 ай бұрын

    wow! so much better than the coreData workflow. So will swift data ever be compatible with older iOS versions or will it only ever be compatible for iOS17 and up? I can't wait until this becomes the standard and we can shove coreData into the abyss. but if it's only ever going to be compatible for iOS17 and above then it's going to take some years right?

  • @ebsm93

    @ebsm93

    9 ай бұрын

    also, Chris - your teaching style is world class. You have the advantage of having a very easy voice to listen to. I really do hope you make a "100 Days of Chris UI" or something in the future

  • @maryannadelman4438
    @maryannadelman443811 ай бұрын

    I have a core data app that I’ll be changing to swift data after seeing this. Question: in my app now, I can click a button in one view that will add item to core, then when I go to view that has the list it will be there. I’m assuming this will be easy to do in swift data also? Not sure how I’ll do that though.

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Hello @maryannadelman4438! To be honest I'm not sure how to answer your question, maybe our community members can help. You can share this to our community forum @ [codecrew.codewithchris.com/] --Joash

  • @hassanportal5865
    @hassanportal586511 ай бұрын

    Hey Chris do you know if Firebase can work with visionOS. It seems there’s only the options for iOS not for anything else

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Hello @hassanportal5865 Unfortunately I'm not sure about the answer. --Joash

  • @chickenrei3876
    @chickenrei387611 ай бұрын

    Hi Chris, can you make a video on how to write neat swiftui and code that can be read by other developers?

  • @MikaelaCaron

    @MikaelaCaron

    10 ай бұрын

    I made a video on this, but it's not specific to SwiftUI, but how to write good Swift code in general kzread.info/dash/bejne/ZpyK29GzhLK5hNI.htmlsi=K8ORdCwzcDNpFPB9

  • @StrugglingIdiot
    @StrugglingIdiot10 ай бұрын

    Nice and Concise. Can I use SwiftData in Vapor (v4.77+), and is it scalable?

  • @CodeWithChris

    @CodeWithChris

    10 ай бұрын

    For everything Vapor related, you should ask our dear friend @MikaelaCaron. She streams on the topic! @AdrienVillez

  • @MikaelaCaron

    @MikaelaCaron

    10 ай бұрын

    Technically you wouldn't use it "in Vapor" but with Vapor. Vapor is for making an API (completely separate from your iOS app), and SwiftData is for storing data locally on the device, as a replacement for CoreData. You'll have to write your own syncing engine to keep the local and cloud data in sync. This is possible, but with SwiftData being so new, it may be tricky. I have never done this, so I can't really say if it's scalable

  • @amith89rm
    @amith89rm6 ай бұрын

    Why is everyone stuck with todo program? There are like millions of example online for todo program. Why not do user login and register and delete user using swiftdata. But thank you for this amazing tutorial.

  • @JustYe322
    @JustYe32211 ай бұрын

    Can you get XCode on MacBooks too, or just the desktop?

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Hi @JustYe322, Yes! Xcode run on any mac. You can also check this link to see the minimum system requirements to be sure that your machine can working on the latest version available. developer.apple.com/support/xcode/ Hope this help. --Joash

  • @pedrojosemoragallegos
    @pedrojosemoragallegos5 ай бұрын

    In more complex applications we usually use different design patterns like the repository pattern and try to move away the functions from the view itself. How would you implement a repository pattern etc. with the SwiftData? :)

  • @CodeWithChris

    @CodeWithChris

    5 ай бұрын

    Hello, Unfortunately, we can't answer you in the comment section but you can try joining and posting in our community: cwc.to/codecrew -- Joash

  • @vanylaskywalker
    @vanylaskywalker9 ай бұрын

    Should we still use codable structs to decode JSON API responses or should we use the new codable @Model class to decode API responses? Which is better?

  • @CodeWithChris

    @CodeWithChris

    9 ай бұрын

    You can actually do both, but if you add the Codable conformance to your SwiftData model, you'll have to manually write the Decodable and Encodable conformances yourself, as derived conformance simply doesn't work out the box. @Model is a Swift Macro, and it adds boilerplate code that does not conform to Codable (that's why derived conformance doesn't work). Donny Wals has written a great article about it, you may want to read about it here: www.donnywals.com/making-your-swiftdata-models-codable/ Have a good day! - Iñaki

  • @derklausi2095

    @derklausi2095

    9 ай бұрын

    @CodeWithChris Hey Chris, as soon as i typ in: "@Model", there comes a red message saying: "@Model requires an initializer be provided for 'DataItem". Please help. What did I do wrong?

  • @adarshgadepalli4847
    @adarshgadepalli484711 ай бұрын

    hey chris im currently a new app dev, can data be stored in firebase as well as locally with swiftdata synchronously?

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Firestore has a local data persistence feature, so you no longer need SwiftData to persist the data that you store in Firestore. - Iñaki

  • @peetorres
    @peetorres10 ай бұрын

    It will have support for CloudKit?

  • @CodeWithChris

    @CodeWithChris

    10 ай бұрын

    "To provide automatic iCloud sync, SwiftData requires the CloudKit entitlement and an iCloud container. For more information, see Configuring iCloud services." For more information, you can check out: developer.apple.com/documentation/swiftdata Thanks for watching! - Iñaki

  • @ruggedd3signz
    @ruggedd3signz4 ай бұрын

    I am getting an error at part 8:07 "Missing argument for parameter 'id' in call" I'm using Xcode 15.0 and iOS 17. Can you explain what I am missing as I am new to swift and swiftdata.

  • @ruggedd3signz

    @ruggedd3signz

    4 ай бұрын

    I was able to figure out the issue with the addItem module had to use *let item = DataItem(id: UUID().uuidString, name: "Test Item")* after that everything else was smooth sailing. Great tutorial on SwiftData. Subscribed and can't wait to watch more videos. Thanks

  • @jr_fin
    @jr_fin10 ай бұрын

    Hey! When I query my database and try to insert the variables like name, it throws up an error saying "Value of type '[my class]' has no member 'name'". Is there something simple I'm missing?

  • @CodeWithChris

    @CodeWithChris

    10 ай бұрын

    Hi! Sorry you're running into that! Have you checked if there is a `name` property in your data model class? You can declare it like how Chris did it on this timestamp: kzread.info/dash/bejne/naaGzc9xp5WYcZs.html Hope that helps! - Iñaki

  • @pnkbrn
    @pnkbrn11 ай бұрын

    Can you use this in a MacOS application?

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Hello @Kyriacos-ue5kt, Indeed, you can utilize the new SwiftData API across all Apple development environments. It's not exclusively limited to iOS. --Joash

  • @VladislavVaz
    @VladislavVaz9 ай бұрын

    can someone help me please .. i have a SWIFTUI application with a flow as follows: A(landscape orientation)->B(landscape)-C(portrait)->D(portrait). also when I navigate back from C to B it should turn back to landscape as B is in landscape. (I am using NavigationLink in my application to navigate)How do I achieve the portrait lock in swiftUI. i tried the .onAppear{} and .onDisappear{} method.. (firstly it is no longer available on ios 16 and secondly it gives a choppy animation where the view does not expand fully to take the new rotated screen size; there is white space after it rotates)

  • @CodeWithChris

    @CodeWithChris

    9 ай бұрын

    Hello, @VladislavVaz! Unfortunately we can't debug it for you in the comment section here but you can try joining and posting in our community: cwc.to/codecrew --Joash

  • @ritzg98
    @ritzg988 ай бұрын

    I get a Preview Crashed error when I try to add a record to the model. Does anyone know why that would happen? I'm pretty sure the code looks the same as Chris's code.

  • @CodeWithChris

    @CodeWithChris

    8 ай бұрын

    Does it show any error in the console log? If it does, you may wanna post about it in our developer community forum: cwc.to/codecrew Someone might be able to help see what's wrong with your code or be able to determine what caused the issue. - Iñaki

  • @arshadshaik2578
    @arshadshaik25786 ай бұрын

    It's very simple than CoreData, can we use in Swift Project, instead of SwiftUI ?

  • @CodeWithChris

    @CodeWithChris

    6 ай бұрын

    Hello, You can still use storyboard with swiftdata. But if you're a complete beginner I suggest you go with swiftui and swiftdata, because many learning resources are based on this technologies. --Joash

  • @arshadshaik2578

    @arshadshaik2578

    6 ай бұрын

    Thanks for your reply@@CodeWithChris

  • @DefeatOG
    @DefeatOG9 ай бұрын

    I keep getting a 0__abort_with_payload error and I've not found anything online that fixes it. Any idea? Thanks for the video!

  • @CodeWithChris

    @CodeWithChris

    9 ай бұрын

    Hello, @DefeatOG! Unfortunately we can't debug it for you in the comment section here but you can try joining and posting in our community: cwc.to/codecrew --Joash

  • @event-subscriber
    @event-subscriber11 ай бұрын

    Similar to useState in react native🤔

  • @w0mblemania

    @w0mblemania

    11 ай бұрын

    Except, actually native. And your app will feel like a real iOS app.

  • @carolinetakam2677
    @carolinetakam267711 ай бұрын

    how would you use AI to automate this building process?

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Unfortunately I'm not sure about the answer, @carolinetakam2677. --Joash

  • @napoleontsar
    @napoleontsar11 ай бұрын

    Until now I don't understand how to create a button that you can click on for moving to another view page in the app..

  • @CodeWithChris

    @CodeWithChris

    11 ай бұрын

    Hello,@napoleontsar! You may want to check this video to learn more about NavigationStack. kzread.info/dash/bejne/aZiitqqrfNysfMY.html&ab_channel=CodeWithChris --Joash

  • @mr.spokesman5849
    @mr.spokesman584914 күн бұрын

    Darn ... too bad this is unusable while working with MVVM, I tried today to somehow bend it to my will ... guess what ... went to core data after ready long struggle.

  • @jmacutube991
    @jmacutube9914 ай бұрын

    When i type in line 29 i get error "Cannot find 'context in scope'. rest of code exactly as you typed it in? strange

  • @CodeWithChris

    @CodeWithChris

    4 ай бұрын

    Hello, Unfortunately, we can't debug it for you in the comment section here but you can try joining and posting in our community: cwc.to/codecrew --Joash

  • @Kai-wu5hd
    @Kai-wu5hd9 ай бұрын

    Can SwiftData work on iOS 15 and above , bro?

  • @CodeWithChris

    @CodeWithChris

    9 ай бұрын

    No, SwiftData is supported in iOS 17+. You'll still have to work with CoreData if you need to support iOS 16 below. - Iñaki

  • @Hshshsusuu62627
    @Hshshsusuu626272 ай бұрын

    At 8:00 when I tap I get a Preview Error message. HELP!!!!!!!

  • @CodeWithChris

    @CodeWithChris

    2 ай бұрын

    Hi, in the video Chris ran the app in the Simulator instead of using the SwiftUI preview. There is an additional code setup required to make the SwiftUI preview work with SwiftData, but it doesn't simulate any persistence functionality that you might expect. To run the app similar to the video, make sure to select a Simulator as your destination (on the top middle part of Xcode), then press Cmd + R. Xcode should build the project and launch the app in the iOS Simulator you chose. Hope that helps! - Iñaki

  • @babida9113
    @babida91139 ай бұрын

    For some reason my Xcode crashes after clicking add items.

  • @CodeWithChris

    @CodeWithChris

    8 ай бұрын

    Hello @babida9113! Unfortunately we can't debug it for you in the comment section here but you can try joining and posting in our community: cwc.to/codecrew

  • @dylan1401

    @dylan1401

    8 ай бұрын

    I had the same problem, were you able to find a solution?

  • @levous2
    @levous25 ай бұрын

    Nice work on separating behavior from view. Too few examples take the care to demonstrate clean code. You, sir, have saved many headaches by teaching good practices

  • @CodeWithChris

    @CodeWithChris

    5 ай бұрын

    Glad you found it helpful! Thanks for learning with us! - Iñaki

  • @REVOLSAMI
    @REVOLSAMI11 ай бұрын

    you misled people by using the swiftlet icon. shouldnt have used it. i was looking for swiftlets video. shame.

  • @Tritoon710
    @Tritoon71017 күн бұрын

    Is it like an embedded database? can you sync data from SwiftData to an online database like MongoDB Atlas?

  • @CodeWithChris

    @CodeWithChris

    16 күн бұрын

    No, SwiftData is not an embedded database, it's an object graph and persistence framework, more like an ORM. It uses SQLite as one of its persistent store. You can sync data from SwiftData and MongoDB Atlas but it requires extra and redundant work, I wouldn't recommend it. SwiftData comes with built-in support for iCloud syncing. If you need to sync data from your app to Mongo Atlas DB, I recommend using Mongo Realm instead of SwiftData (www.mongodb.com/docs/atlas/device-sdks/sdk/swift/). - Iñaki

  • @Tritoon710

    @Tritoon710

    16 күн бұрын

    @@CodeWithChris Many thanks for the detailed answer 🙏🏻