SwiftUI 2.0: Core Data - How To Use Core Data From Scratch (2020)

Поділитися
Вставка
  • Опубліковано 11 вер 2024
  • In this tutorial you will learn how to use Core Data with SwiftUI 2.0 from scratch. First you learn about Core Data and its Core Data Stack, then you learn how to create a basic app with CRUD (= Create, Read, Update and Delete) capabilities using SwiftUI and Core Data.

КОМЕНТАРІ • 129

  • @khram84
    @khram84 3 роки тому +5

    Any idea why the "preview/canvas" doesn't run? We have to run it in the simulator. Is it an Xcode bug?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +7

      Good question! It is not an Xcode bug. The preview relies on the "ContentView_Previews" and not the "ContentView". We have to set the managedObjectContext environment for the "ContentView_Previews" as well. So you could simply add: ".environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)" after the "ContentView()" in "ContentView_Previews". That should fix your problem.
      In practice this is quite nice because it also allows you to use a different viewContext for previews if you want.

    • @khram84
      @khram84 3 роки тому +1

      @@BeyondOnesAndZeros Thank you!

    • @khram84
      @khram84 3 роки тому

      @BeyondOnesAndZeros its odd, I added the code by copying and pasting it into "Contview.swift" in "ContentView_Previews" under "ContentView()", I am getting an error "Cannot PresistenceController in Scope"? I can see that exact in "ToDosListApp". What I am not getting here?

    • @khram84
      @khram84 3 роки тому

      Never mind, don't wast your time... I should read more thoroughly... I was putting the code below "ContentView()" instead of after it.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      ​@@khram84 You're right, it should go right after it. Glad you sorted it out! :)

  • @kennethmerenda5046
    @kennethmerenda5046 3 роки тому +10

    This was a great video. I've been trying to find Core Data tutorials, but most of the ones out now are based on Xcode 11.x and earlier, with App and Scene delegate files. Your video was the first I found that used the new SwiftUI lifecycle and implemented the stack with Persistence.swift. Thanks!

  • @WilfClegg
    @WilfClegg 2 роки тому

    I've followed at least 10 tutorials on SwiftUI and Core Data. This is the first tutorial that showed me how to do an "update". None of the other videos included the update function for Core Data. Thank you, well done...and excellent explanations throughout the tutorial! Now I will take what you have taught me, and reformat it into an MVVM structured xCode framework. Beautiful!

  • @LeonaS-jd2wy
    @LeonaS-jd2wy 2 роки тому

    I've watched 3 to 4 Core Data tutorials, and this is the most and only clear one. Thank you and please keep making these high-quality tutorials.

  • @joeprince7509
    @joeprince7509 4 роки тому +7

    Your deserve way more subscribers, thanks for continuing to publish these great videos!

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  4 роки тому +1

      Thank you, Joe! Good to hear that these videos are helpful. I really appreciate the support :)

    • @khram84
      @khram84 4 роки тому +1

      BeyondOnesAndZeros 100% agree with Joe. Hope we can help spread the word!

  • @ganeshmahajan1985
    @ganeshmahajan1985 3 роки тому +11

    This is fantastic tutorial! Would you please create one with MVVM + Core Data as well ? Thanks a ton again! 👏🏻👏🏻👏🏻

  • @mariomatschgi344
    @mariomatschgi344 2 роки тому +2

    OMG thank you so much for this nice tutorial!
    All other tutorials i found were either too old or just way to complicated - but yours is just perfekt!
    Have some cookies for your nice efforts 🍪🍪🍪

  • @batuhankrbb
    @batuhankrbb 3 роки тому +2

    Bro, I'm in love with your tutorials. Whenever I see that you made a tutorial for the topic which I'm looking for, I feel amazing because you explained everything very well. Keep going!

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Thanks Batuhan, glad you find them helpful! appreciate the support :)

  • @bjesusmoises
    @bjesusmoises Рік тому

    This is the best tutorial that I found in YT, thanks!

  • @matthew78uk
    @matthew78uk 3 роки тому +1

    Very helpful and well planned, many thanks!

  • @bssgkmsd8
    @bssgkmsd8 3 роки тому

    I've watched other core data videos. Your explanation is 100% better than all of them.
    Thank you for doing such a great video.

  • @hcri
    @hcri 3 роки тому

    I have been following you for awhile and you have contributed allot for your viewers. I count myself lucky to be able to learn from your tutorials. One course you should think about would be JSON to Core Data.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Hi Robert, I appreciate your support! I have a question regarding your request.. are you suggesting a tutorial on loading a JSON from the web and storing it on the client as some kind of caching mechanism?

  • @JamesHaville
    @JamesHaville 3 роки тому

    Finally got round to watching this - very nice summary! Thank you.

  • @arsenthompson
    @arsenthompson 2 роки тому

    Thaaaankss!! Brilliant tutorial for work DB + UI, amazing work)

  • @CVHVisualsTutorials
    @CVHVisualsTutorials 3 роки тому

    Great tutorials! Much appreciated. I will let the communities be aware of your Channel.

  • @ozanmuyes
    @ozanmuyes 3 роки тому

    Thanks for the through explanation and demoing. It was the first time I watched your video and I'm immediately sold 🙂 Going to watch other videos of yours. If it's not much to ask though can you use the dark theme?

  • @THE_ONLY_GOD
    @THE_ONLY_GOD 2 роки тому

    Thanks! Links to the code on github or another repository would be appreciated. (mainly for notes because sometimes screen didn’t show entire file content). Thanks in advance for editing the description to post that code.

  • @maratkhanbekov9964
    @maratkhanbekov9964 3 роки тому

    Thank you very much for such a simple explanation!

  • @andrejkling3886
    @andrejkling3886 2 роки тому +1

    Thank you so much... Go Beyond

  • @dirrren5770
    @dirrren5770 3 роки тому

    Thank you so much for clear and concise explaination on SwiftUI 2.0 core data!

  • @jovictor3007
    @jovictor3007 3 роки тому

    Thank you , this is way more easier and less code than using property list to make a crud list app.

  • @thebrettlarson
    @thebrettlarson 3 роки тому

    I stand corrected (and helped!)... This works! Great video.

  • @joltguy
    @joltguy 3 роки тому +1

    Thanks for this great tutorial! Covered all the basics in a clear and understandable way. Would love to see how this can be adapted to work with CloudKit!

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Thanks, Jeff! Good suggestion, it's already on my list. I will get to it as soon as possible :)

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      Hi Jeff, I finally got around to create the video on CloudKit: ua-cam.com/video/F5B6fbTAl04/v-deo.html enjoy :)

    • @joltguy
      @joltguy 3 роки тому

      @@BeyondOnesAndZeros Thank you! Checking it out now...

  • @NTuneLabs
    @NTuneLabs 2 роки тому

    This was great. Maybe update for Xcode 13 and show a products based model, say for invoicing... just an idea.

  • @AlbCaphalor
    @AlbCaphalor 2 роки тому

    Excellent tutorial, thanks

  • @AbbeyOnAtkins
    @AbbeyOnAtkins 3 роки тому

    Concisely explained. Well done!!

  • @FckYouPlz
    @FckYouPlz 3 роки тому

    Great tutorial! The logics though need to be inside a ViewModel

  • @qsaluan
    @qsaluan 3 роки тому

    Phenomenal tutorial! Thank you so much for your work on this.

  • @dmitrygolovin8066
    @dmitrygolovin8066 2 роки тому

    Thanks for the awesome content!

  • @BlueMooseOnFire
    @BlueMooseOnFire 3 роки тому

    What an amazing tutorial!!! Much appreciated. I had one question. Is it possible to test this on a live iOS device? When I switch the simulator to my actual device it complains about Task not being in scope. Is this a bug or can you not test core data application on a live device?

  • @johnr.5475
    @johnr.5475 3 роки тому

    A great help. Thanks

  • @anandsunku
    @anandsunku 3 роки тому

    why does fetchresults does not require ViewContext but Contructor of Task in addTask does require one ?

  • @kasirajan2289
    @kasirajan2289 3 роки тому +1

    great work.. its very simple. Thanks..

  • @gregohb
    @gregohb 3 роки тому

    The app builds then crashes. it seems that the database is empty and possibly the pointer is negative to start.
    Fatal error: Unresolved error: nil: file CoreData2/Persistence.swift, line 18
    2021-03-20 14:52:50.860348-0700 CoreData2[42026:4013387] Fatal error: Unresolved error: nil: file CoreData2/Persistence.swift, line 18
    (lldb).
    Line 18 is this : if let error = error as? NSError? { fatalError("Unresolved error: \(error)")}
    Am I missing something? I have spent an hour on this, and gone over the video twice to make sure its precisely the same (AFAIK)

  • @user-er8ep4ik3h
    @user-er8ep4ik3h 3 роки тому

    Thank you !! However, NSManagedObject is a class rather than a protocol , right ?

  • @annaovesna4440
    @annaovesna4440 3 роки тому

    Shutting down and launching Xcode again doesn't solve the "Cannot find type 'Task' in scope" for me, don't you have any other tip please??

  • @kevinbaize8574
    @kevinbaize8574 3 роки тому

    CoreData: error: +[Task entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
    Fatal error: UnsafeRawBufferPointer with negative count: file Swift/UnsafeRawBufferPointer.swift, line 872
    2021-03-02 18:01:01.704378-0800 ToDO[6139:143889] Fatal error: UnsafeRawBufferPointer with negative count: file Swift/UnsafeRawBufferPointer.swift, line 872
    I get this error above and not sure why. You have any idea?

  • @chris_bennett
    @chris_bennett 3 роки тому

    Great tutorial, this helped me a ton - thank you so much!
    I have one question though: I want to move all the CRUD functions for the tasks into a TaskManager class because I want to keep my views as clean as possible. This works great, except for the part about updating.
    My method inside the TaskManager class is func update(_ shoppingItem: FetchedResults.Element, withTitle title: String) and I'm getting the error "Cannot find type 'FetchedResults' in scope". Any hints as to what I am missing? Passing the Task using the method call from the view does not seem to do the trick...

  • @ryanbell5213
    @ryanbell5213 3 роки тому

    If I wanted to do a MVVM architecture, would I put the...
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(sortDescriptors: [])
    @Published var list: FetchedResults
    in the ViewModel??

  • @poornabhaskarduvvari6436
    @poornabhaskarduvvari6436 3 роки тому +1

    Great Explanation👍

  • @reece5863
    @reece5863 3 роки тому

    At 13:40 xcode did not want to work with .environment. I had to restart xcode fresh and it all runs now. Maybe someone else has this issue...

  • @hallo_ween07
    @hallo_ween07 2 роки тому

    HELP I am getting this error message CoreDataNSManagedObject must have valid NSEntityDescription. What should I do i am a beginner! Thanks 🙏

  • @hsksearch3
    @hsksearch3 3 роки тому

    Great!!!!

  • @saulramirez727
    @saulramirez727 3 роки тому

    Insane buddy. Thanks a lot

  • @mykytadubovik4993
    @mykytadubovik4993 3 роки тому

    Fine, thank you!

  • @gandarainpanjaitan8595
    @gandarainpanjaitan8595 3 роки тому

    Hi, thanks for this tutorial.
    I have an question, how to move fetch from core data into model?

  • @Nunny25
    @Nunny25 3 роки тому

    Hi, thanks for tutorial. How much record can be saved in Core data? It is suitable for large databases with thousands records?

  • @hsksearch3
    @hsksearch3 3 роки тому

    How to delete all tasks in one time? ^^ thank you

  • @jiren8991
    @jiren8991 3 роки тому

    Thanks a lot

  • @batissemanhardt8728
    @batissemanhardt8728 3 роки тому

    How would we have it so we can customize the tasks with a textfield?

  • @vixell114
    @vixell114 3 роки тому

    Sztosiwo! thx!

  • @draytond
    @draytond 3 роки тому

    Excellent tutorial! Very clearly explained.
    Could you use a Dark Theme for Xcode such as Midnight?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      Thank you, Drayton! Sure, I will consider the dark theme for future videos :)

    • @draytond
      @draytond 3 роки тому +1

      @@BeyondOnesAndZeros Looking forward to it!

  • @ronnie149
    @ronnie149 3 роки тому

    I use a Post NSManagedObject class for some posts.. i use that post class for my main Feed View, for my Favorites View and my Profile View... how do i only save the Posts that use in my profile view. Do i need a separate context? A separate container? Maybe a coordinator? Can someone help me on this one

  • @nomonomadbisonware9218
    @nomonomadbisonware9218 3 роки тому

    I have two entities with a `Position` entity having a "to-many" relationship to an `Order` entity. I'm assuming I'd need to use an NSPredicate to filter the orders fetched by the matching position? I know the position entity has a property of type orders but it doesn't seem that accessing that property allows for smoother, natural animation like the fetch request process does. Would you be willing to create a video using relationships in CoreData and using NSPredicates?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Hi there, that’s an interesting example. I will cover entity relationships in more depth in a separate video maybe it will help you to solve your problem.

  • @reshetn1koff
    @reshetn1koff 3 роки тому

    Nice tutorial. Thanks

  • @irsyadashari5536
    @irsyadashari5536 3 роки тому

    how do you know all of these stuffs ?

  • @ktmman1503
    @ktmman1503 3 роки тому

    Brilliant .. great work indeed and thank you !... any chance of adding to your code, so that the user can add some text etc.. and also... incorporate iCloud too ? Thanks... Craig.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      Thanks, Craig! That’s a great idea, I will cover these topics very soon in a follow up video.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      fyi. there is now a video on CloudKit that covers the part on how to incorporate iCloud: ua-cam.com/video/F5B6fbTAl04/v-deo.html

  • @dc5mike
    @dc5mike 3 роки тому +1

    Do you have any source code repositories for your examples?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      Hi Michael, as of now I do not have a repo for my examples. But I will try to set one up and will get back to you

    • @dc5mike
      @dc5mike 3 роки тому

      @@BeyondOnesAndZeros any luck?

  • @quark67000
    @quark67000 3 роки тому

    How do you go in line 18 from 11:39 state (with the text "completionHandler") to the 11:41 state (without "completionHandler", but with "in code") ? A cut in the video? A shortcut? Where is the explanation, please?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      Hi quark67000, it’s a nice and handy Xcode auto completion feature. You access the auto completion by highlighting the spot that you want to “auto-complete” using the “Tab” keyboard key, and pressing the “return” keyboard key to replace the placeholder with the suggested auto-completion.

    • @quark67000
      @quark67000 3 роки тому +1

      @@BeyondOnesAndZeros Thanks very much, I wasn't aware of this feature! Great!

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      @@quark67000 You're welcome :)

  • @user-rc2nt1tb2u
    @user-rc2nt1tb2u 3 роки тому

    Hi ,
    My app is so slow when launching, and it says that is more than 40 tasks , how can i fix this?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Hi Thuraya, are your launching your app on the simulator or a real device? Also it would be interesting to know the specs of your hardware that runs Xcode. Nevertheless, the simple code in this example should not lead to any crucial performance bottleneck.

  • @smartapps8770
    @smartapps8770 3 роки тому

    I have this error in Xcode12:
    RemoteHumanReadableError: Failed to update preview.
    The preview process appears to have crashed.
    Error encountered when sending 'previewInstances' message to agent.
    ==================================
    | RemoteHumanReadableError: The operation couldn’t be completed. (BSServiceConnectionErrorDomain error 3.)
    |
    | BSServiceConnectionErrorDomain (3):
    | ==BSErrorCodeDescription: OperationFailed

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Hi, please see the answer to the pinned comment. You should be able to solve this by simply setting the environmentObject to the preview.

    • @smartapps8770
      @smartapps8770 3 роки тому

      @@BeyondOnesAndZeros I already have it, but still failed:
      struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
      ContentView()
      .environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
      }
      }

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      This seems to be a common issue with the new Xcode version. Did you add anything on top of what was shown in the video? Feel free to send me your project and I will check if it compiles on mine :)

  • @user-rc2nt1tb2u
    @user-rc2nt1tb2u 3 роки тому

    Hi,
    Is it okay to have more than one big array (not json) of texts , colors , or images in my app(because i write it my own ) , and if not with what i can replace it?
    Thanks.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Hi Thuraya,
      Can you give me an example of what you are trying to achieve? Do you mean by writing “your own”, your own custom types?

    • @user-rc2nt1tb2u
      @user-rc2nt1tb2u 3 роки тому

      I'm made an array that has more than 100 questions , and i wrote these questions my self.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      It depends. If you have an app where you as the user create the questions using the app, it is totally fine. However, if you are creating these questions in code, it would be much more maintainable if you would load these initial questions from a JSON.

    • @user-rc2nt1tb2u
      @user-rc2nt1tb2u 3 роки тому +1

      Thank you 🙏

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      You’re welcome :)

  • @advancedgame7362
    @advancedgame7362 3 роки тому

    Sorry but I have this error: [error] error: Failed to load model named TodoList

    • @advancedgame7362
      @advancedgame7362 3 роки тому

      I solved, I insert the wrong name in NSPersistenceContainer arguments. Great guide

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Awesome, glad you figured it out! :)

  • @user-rc2nt1tb2u
    @user-rc2nt1tb2u 3 роки тому

    Where can i find the (App.swift ), the file with the @main keyword ?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      Hi Thuraya, it’s part of the new Xcode Template. Create a new Xcode project and choose “App”, and in the next screen where you define the project name make sure to set the “Life Cycle” to “SwiftUI App”. If you do not see the option you’re probably running an older version of Xcode.

    • @kelvinjou4582
      @kelvinjou4582 3 роки тому

      yeah, I think you're still using the appDelegate method

  • @mhfs61
    @mhfs61 3 роки тому

    👍🏼👍🏼👍🏼

  • @sipanhazim4128
    @sipanhazim4128 3 роки тому

    why you don't have unlike woow 😍😂

  • @SebastiansBlog23
    @SebastiansBlog23 3 роки тому

    hey very good instructions. it doesn't work for me can i send you my code?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      Thanks Sebastian. Sure, drop me a mail at beyondonesandzeros[at]gmail[dot]com

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      Hi Sebastian, I had a look and found out where your issue is. You are calling the ".navigationTitle("Todo List")" and ".navigationBarItems(trailing: Button("Add Taskt").." on the NavigationView but it should be called on the ending curly bracket of the List instead. That should fix your problem, give it a try and let me know :)

    • @SebastiansBlog23
      @SebastiansBlog23 3 роки тому

      @@BeyondOnesAndZeros Hey Beyond, it works :-) Thank you very much

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      @@SebastiansBlog23 Awesome! you're welcome :)

  • @thebrettlarson
    @thebrettlarson 3 роки тому

    This didn't work...

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Hi Brett, do you mind sharing where you’re stuck and what error message you are getting?

    • @thebrettlarson
      @thebrettlarson 3 роки тому

      @@BeyondOnesAndZeros As soon as I build the project, it launches the simulator and crashes with an error "container.loadPersistentStores { (storeDescription, error)... Right around the 20 minute mark in the video if you are following along.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому

      Your Xcode logs should show sth after: "Unresolved error: ". That’s the interesting part, that could help us to find the issue. Feel free to send me your current project and I will have a look at it :) beyondonesandzeros at gmail dot com

    • @thebrettlarson
      @thebrettlarson 3 роки тому +1

      @@BeyondOnesAndZeros That's amazing. I will send it to you and really appreciate your quick response and help! It's a great video and I was frustrated to hit the wall.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 роки тому +1

      @@thebrettlarson I had a look at your source code. The issue is in the errorHandling of your PersistenceController.swift file. Please replace: "if let error = error as? NSError?" with "if let error = error as NSError?". Your current definition basically holds always true and throws a fatal error, even if everything is fine :) I hope this fixes your problem, if not let me know and I will have another look.