How to safely work with Core Data on a background thread

Поділитися
Вставка
  • Опубліковано 16 лис 2024

КОМЕНТАРІ • 46

  • @swiftarcade7632
    @swiftarcade7632  4 роки тому +2

    Got any other questions about Core Data? Let me know below.

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

      I am getting this crash "CoreData _PFObjectIDFastHash64" only in iOS 14. Can you help me to resolve this issue.

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

      @@1ongole1 Looks like it is a known bug in Xcode 12.4. Check this out and let me know if this doesn't solve the problem developer.apple.com/forums/thread/662240

    • @1ongole1
      @1ongole1 3 роки тому

      @@swiftarcade7632 I gone through this thread and tried all the possible solution in that thread. But no luck ...

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

      @@1ongole1 Sorry Monish. I haven't hit this problem yet but it looks like it is a problem for everyone. One thing you could try (until it is fixed) is see if you can lower your iOS version to a level where that goes away. Not ideal I know. But it may get you by until Apple can fix. Sorry I can't be of more help. :( Apple should fix this ASAP.

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

      This is quite mind opening video. Thanks for it. I am working with iCloud and Coredata. Whenever I create or delete data it works properly on IOS simulator. When I add data on ios app, it also adds on MacOS app as well. But once I delete row of List on either ios app or macos app, it deletes it on MacOS app but UI of MacOS remain unresponsive, another way of saying it freezes the macosapp. I debugged it on MacOS app, it works on main thread. I run into this useful video, I believe I should make fetching operation on background thread. But how? Because I do not want to create an object as you did on video. I just want to delete row and keep continue to use app as it is.

  • @CielLearcen
    @CielLearcen 4 роки тому +5

    Greetings from Russia. Really appreciate your work. Especially when i got my first job as jr ios dev your channel helps me a lot

    • @swiftarcade7632
      @swiftarcade7632  4 роки тому +2

      DevKit! Congratulations - that is awesome. Welcome to the world of iOS development. Lots more to come. Glad you enjoyed the channel. Best of luck on the new job. Let us know how you are doing and what subjects interest you most. All the best.

    • @CielLearcen
      @CielLearcen 4 роки тому

      @@swiftarcade7632 Yeah, sure. Thank you!

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

    John is amazing! I love the way you explain things!

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

    The video is awesome, thanks for share your knowledge!

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

    Amazing....fantastic way of explanation 👍

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

    This is what I was looking for

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

    This was really useful, thank you so much 😎

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

    This was really helpful. Thank you so much. 😊😊😊😊😊🤗🤗🤗

  • @ethanyang961
    @ethanyang961 8 місяців тому

    thank you very much!

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

    This was really useful, Thank you so much ❤️

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

      Most welcome Ankit. Thanks so much for stopping by.

  • @arnosolo2008
    @arnosolo2008 Місяць тому

    Thanks you

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

    Very good, thank you so much!

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

    This is quite mind opening video. Thanks for it. I am working with iCloud and Coredata. Whenever I create or delete data it works properly on IOS simulator. When I add data on ios app, it also adds on MacOS app as well. But once I delete row of List on either ios app or macos app, it deletes it on MacOS app but UI of MacOS remain unresponsive, another way of saying it freezes the macosapp. I debugged it on MacOS app, it works on main thread. I run into this useful video, I believe I should make fetching operation on background thread. But how? Because I do not want to create an object as you did on video. I just want to delete row and keep continue to use app as it is.

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

      Hi Eva. That is a tough one to solve. Sounds like you may have a refresh problem. If you know it is being deleted in CoreData, see if you can't get an event or notification from when that happens. Then you can update the UI. Good luck!

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

    You are amazing

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

    This helps, thanks

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

    May I ask a few questions? In timeline 2:22, you didn't put your code in background thread, but your printStats() func printed "off main thread", I tried on my side, it is always on main thread. How to ensures my background Core Data updates are all performed on same single thread? And another one is could we use GCD on Core Data like `DispatchQueue.global().async {}`.

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

      Great question Film Reporter. The reason we are on the background thread here is because my URLSession call put us there. URLSession often does work on the background thread. So if we dispatch back via protocol delegate, when I do to printStats, I am on the background thread at that point. Thats all. Thanks for asking!

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

      @@swiftarcade7632 Thanks for replying me. I get your point, but since the call of CoreData is inside URL session request closure, does that mean it is always in background thread? Then we don't need to put it in background thread in `createCoreDataWeatjer`again.

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

      @@ZhouHaibo I see what you are saying now. Yes - I think you are right. The reason why it is good however, to still put the CoreData work on a background task is because you never know where that func is going to be called from. i.e it could be called from a main UI thread function. In which case to be safe you'd want to put it on a background thread. So yes. I believe you are right. Still good to call on background however in case you are invoking it from main thread. Great question!

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

      @@swiftarcade7632 Thank you, I get that. May I know how to transition NSManagedObject instances between background thread and main thread. Let's say in your example, you may fetch the weather in viewDidLoad in background thread and when you add a new weather, you update it in main thread. But how to transition that data between background thread and main thread. I think your video doesn't show off it.

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

      @@ZhouHaibo Checkout explanation at 3:35. We basically ask our persistentContainer for its view context (main thread), and the call context.perform (which will do work on main thread because that is where the view context came).

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

    Great stuff! I got an issue though. I'm saving all my core data on a backgroundContext. On my SwiftUI view I'm using a @FetchRequest for my core data but this does not reflect changes I do on the backgroundContext. If I save using viewContext my view updates as well. How can I fix this? Thanks!

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

      So as far as I, everything should work the same way between UIKIt and SwiftUI and @FetechRequest. My advice would be to create a simple standalone app, get it working there, and then app it to the real thing. Good luck!

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

      @@swiftarcade7632 Thanks for the quick reply! I think I figured it out. This seemed to do the trick: viewContext.automaticallyMergesChangesFromParent = true

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

    Awesome 👍🏻

  • @roman1277
    @roman1277 4 роки тому

    Why haven't you used background context?

    • @swiftarcade7632
      @swiftarcade7632  4 роки тому

      Hi Roman. Thank you for the question. Do you mean private queue? We could definitely do it that way too. Cheers.