Download JSON from API in Swift with Combine | Continued Learning #23

Поділитися
Вставка
  • Опубліковано 25 кві 2021
  • In this video we jump in to the excited world of Combine! Combine is a new framework from Apple that is made specifically for handling asynchronous events. By using Publishers and Subscribers, we are able to constantly keep out data in sync across our application. At the end of this video, we will compare the code to the last video (doing the same with but without Combine) to highlight the benefits of using this method. In the next few videos we will continue exploring this new framework!
    *Previously covered topics that we use herein*
    Background threads: • Multi-threading with b...
    Weak Self: • How to use weak self i...
    Codable: • Codable, Decodable, an...
    *Links used in video*
    Link 1: jsonplaceholder.typicode.com/...
    Link 2: app.quicktype.io
    Next video: • How to use Timer and o...
    Last video: • Download JSON from API...
    🤙 WELCOME BACK 🤙
    WEBSITE: www.swiftful-thinking.com
    DISCORD: / discord
    GITHUB: github.com/SwiftfulThinking/
    SAY THANKS: www.buymeacoffee.com/nicksarno

КОМЕНТАРІ • 86

  • @hakamgo3894
    @hakamgo3894 4 місяці тому +2

    Best and cleanest tutorials ever. especially for Swift, because there aren't too many tutorials out there!

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

    Excellent. I've been following you for a few days and I enjoy the clarity of your work. Very neat and concise.

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

      Thanks for watching Patrick! Let me know if you have any questions :)

  • @hamidevtech
    @hamidevtech Рік тому +2

    Very thorough and concise. Completely understandable 👌.

  • @indnjcyoung
    @indnjcyoung Рік тому +2

    this is really cool. Maybe the best SWIFT education clip I've ever seen. The subscribe - publish analogy is incredibly engaging.

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

    You are a boon to swift world. The way the subscription logic was explained was extremly helpful and something so easy to remember and follow. Thank you so much 🙌🙌🙌

  • @irvingchen7072
    @irvingchen7072 2 роки тому +7

    This is a god tier tutorial man!! Clear explanation, great analogies. It feels like a crime learning all these for free, so I just bought u a couple coffees :D. You are so under-appreciated!!

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

      Haha thanks for watching Irving 😎 This comment made my day!

  • @gurcangulec
    @gurcangulec Рік тому +3

    Your videos are awesome Nick! This channel is a hidden gem. Thanks a lot!

  • @Jump_Jay
    @Jump_Jay Рік тому +1

    Thank you for this very detailed explanation of Combine!!!! 😁

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

    Thank you for all yo do! we're learning so much from you.

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

    Awesome video. I really appreciate you putting in the logical steps. I actually comment my code with the steps in the order of how the video plays out. I was stuck for 10 mins or so as I wasn't receiving any data but I was missing a period prior to "font(.headline)" newbie mistake!

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

    Very helpful Nick, amazing as always in your explanation. 👏

  • @MohamedAli-kg5im
    @MohamedAli-kg5im 5 місяців тому

    Fantastic vidio, I am so grateful to you, Nick

  • @user-a_fellow_traveler
    @user-a_fellow_traveler Рік тому

    thanks a lot, this video is very useful for people who just start using combine

  • @_arun_k
    @_arun_k 9 місяців тому

    Very practical example thank you

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

    Great job!! i love it... thanks 😍

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

    Love the videos man....I just have to start typing out the code to grasp it even more

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

      Hi David, Do you just watch them? I think most people type alongside haha

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

      @@SwiftfulThinking watch, watch again pausing to type it out, then watch again

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

      @@SwiftfulThinking I always type along because then if I get confused how something works I can change a thing or two and see what end may up happening, I’m pretty sure most people do that (or should)

  • @Wusagi-gi5xk
    @Wusagi-gi5xk 5 місяців тому

    Thank you so much...

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

    you make life easier

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

    Amazing video, .. thanks

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

    Your videos need more views !!!
    Would love to see a firestore + Combine video :)

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

      Firestore + Combine is a powerful combo. Coming soon 😎

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

    thank you so much!

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

    Still with you. You’re doing great.

  • @OrtoInScatola
    @OrtoInScatola 6 місяців тому

    Hi Nick! A beautiful series of videos you put together!
    I do have one question, though: Is this completely replaced by the new Observable framework or do these two frameworks go side by side?

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

    👏👏👏👏👏

  • @kal5211
    @kal5211 Рік тому +1

    My man.

  • @gucp
    @gucp Рік тому +2

    5:11 Be careful when using quicktype app. As you can see in the video, the struct created let userID instead of userId (capital letters matter). To make sure it generates the correct struct, go to Other and then select “original” in the box that is set as “Pascal”. I run into this problem on the last video and it was driving me crazy because the data was not appending 😩

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

      Thanks for calling this out! Super helpful.

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

    So it's possible to handle WebSockets and HTTP networking in a single style. Back in the days when I was learning SwiftUI from scratch, I tried using combine but, if have remembered correctly it works only in classes with extended ObservedObject protocol. I usually follow MVVM so I separate my service layer. So is it still doable? I was trying to make T type parser so i can handle all requests in a single function just by changing the model and URL. Will try and update this post. Anyway thanks for the info Nick! U are a gem!

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

    Hey Nick, Thanks a lot for the tutorials!
    I just have a question, is there anyway we can use this same approach to make a general purpose downloader function like we did in the @escaping video?. I used your approach in the previous video and created a NetworkManager class with a singltone and put the function there so we can use it across the app. I would love to be able to do the same with Combine.

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

      Yea, you can make another modular function that returns the publisher. I'll have to do it in another video haha

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

      Yeah, that would be really helpful to have a more generalized version that wasn't a dataTaskPublisher. Combine seems like it's still pretty tricky for many. Maybe just me!

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

    Thank you! Just trying to follow along and getting errors. It is on my end or video needs to be updated (Value of type 'URLResponse' has no member 'statusCode' and Decodable class)

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

    Great job, still a little complicated for me, but will keep at it till I get the hang of it.
    Would it be something similar where if a remote JSON file changes, the user gets notified? Or this would be in the Apple Push Notifications Service?

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

      Don't worry if you're still a little confused. We're going to use this a bunch of times in upcoming videos so you'll become an expert soon haha.

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

      In this video, we are just fetching the data and returning back to our app. We are NOT listening to the server for updates. In order to do that, we would need to implement our own backend. If we wanted to listen to the backend and give the user notifications while the app is OPEN, we do not need a Apple Push Notifications Service. However, if we want to listen to the backend and give the user notifications while the app is closed, then we would need to implement APNS.

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

    A little difficult to apprehend, but the explanation is clear.

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

      We're going to use this a lot in the coming playlists so you'll get used to it! Don't worry haha. Combine is VERY powerful.

  • @dre5671
    @dre5671 Рік тому +1

    Hey Nick, I have a question I hope you can answer, but first want to say THANK YOU for all of your amazing content! You're my go to for SwiftUI.
    My question is, how do we incorporate decoding strategies using combine? I want to change the properties from snake_case to CamelCase. I'm having difficulty finding a solution. Thanks!!

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

      The JSONDecoder has a keyDecodingStrategy that you can set

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

      @@SwiftfulThinking Right, I'm trying figure out where/how to. Is it as simple as adding
      JSONDecoder().keyDecodingStrategy = .convertFromSnakeCase
      to the end of the URLSession publisher?
      Thanks!

    • @SwiftfulThinking
      @SwiftfulThinking  Рік тому +1

      @@dre5671 I would initialize it somewhere else, like in the parent class or something and then pass the reference into the publisher

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

    All double-dutch to me, this one!

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

    Around line 62 you have:
    .sink(receiveValue: { [weak self] (returnedPosts) in
    Why the parentheses around (returnedPosts)?
    The code seems to work the same without the parentheses:
    .sink(receiveValue: { [weak self] returnedPosts in
    ...just curious if I'm missing something.
    Thanka

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

      No this might just depend which version of Swift you are running. I don't think this will make a difference haha. It's probably cleaner to do it without parenthesis if possible. Although if you use more complex return types, you may have to add them back in!

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

    Can you please make a tutorial on Post Requests

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

    Is there a playlist for Combine tutorials ? I need from intro level onwards..

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

      No, I didn't make a separate Combine playlist. This is the first video I introduced the subject and we will gradually start using it in future videos!

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

      @@SwiftfulThinking Thank you that was very helpful. What else can Combine framework accomplish? This was my first ever Combine intro, very informative.

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

    Hey Nick, How do we parse this JSON file and save it in CoreData, so that it is still available for offline users? Thank you buddy.

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

      Hi Irfan, there are a few options here. Firstly, if you are going to parse the JSON file, you are probably converting it into some type of Model. In that case, you would create a Core Data entity for that model and save the entities. However, if you don't want to parse the JSON file, you could save the entire JSON as a file to the FileManager (I cover that in video #26)!

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

      Hi Nick, Would there be a difference in performance between using a saved JSON vs data in CoreData?

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

    if you know what you're doing... hahaha Back to Bootcamp!

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

    I hope you will show how upload data with combine too

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

      Hi Eric! To do this the right way, we need a database to upload to and some more advanced Combine methods that I haven't covered yet. I will definitely be doing this but it will probably be in a following playlist that is more advanced level. So hopefully in a few weeks! Thanks for watching.

  • @soulmood5893
    @soulmood5893 Рік тому +1

    What is the difference between [PostModel.self] and [PostModel].self ? So confused

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

      here you are decoding the json from the url, you should ask yourself this,
      what am I decoding?
      if the json is an array of objects then it should be an array of whatever model you are trying to decode, in this example it was an array of posts which you already modeled as Post that's why it's [Post].self
      but if you are decoding one object as we did in the last video, then it should be Post.self
      So always check what is the JSON is giving you!
      one object or array of objects and so on, and then decode the json accordingly

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

    I think it's odd that this is a video about using Combine, and yet, you didn't even need to import Combine until you wanted code-completion for "AnyCancellable" which wasn't even actually used, (to cancel). So, could you have done this whole example without even using, (importing), Combine?
    I did try commenting out "Import Combine" and the two lines pertaining to "AnyCancellable", e.g. .store(in: &cancellables), and the code compiled fine but the the rendering of the UI was blank.
    ...so I kinda answered my own question, but I'm still puzzled as to what kind of magic is happening here.

    • @SwiftfulThinking
      @SwiftfulThinking  3 роки тому +3

      Haha I know it feels like we barely needed combine, but the magic comes from AnyCancellable. This lets us store subscriptions so that they can stay "alive" until cancelled. If you don't store the subscriptions anywhere, they will be deallocated from memory immediately after being created.

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

      Great question Jerry, glad you asked it

  • @vazzhh
    @vazzhh 4 місяці тому

    Hello. I have a question. In the 17:31 minute that you say it's in an Array [ ], I've seen other json and they only have { }. How can I make it work?
    gracias. !!!!

    • @SwiftfulThinking
      @SwiftfulThinking  4 місяці тому +1

      Then it would be decoding to PostModel.self instead of [PostModel].self

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

    How do we cancel the publisher if we want to?

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

      If we store the subscription as a single AnyCancellable, then we can cancel it directly. Since we're storing it in a Set of AnyCancellables, we have to either loop through the Set and cancel all subscriptions or not cancel it (it's not that big of a memory allocation to keep an extra subscription alive). However, you can also create a custom type that takes AnyCancellable + an ID and then store that as a Set instead, and delete based on the ID.

  • @jamesbush7717
    @jamesbush7717 7 місяців тому +1

    ‘i’ before ‘e’ except after ‘c’

    • @ericbarnes6362
      @ericbarnes6362 3 місяці тому

      you just solved a bug in my brain. Thanks

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

    If you understand... great question, answer NO. Not at the moment. Maybe after a dozen rewinds I'll see something. No chance of me being a great SwiftUI programmer, your job is secure.

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

    Hi! Thanks a lot for your work, I really appreciate it. Are you planning to make some video about new (Async, Await) opportunity in swift?

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

      Haha everyone's been asking for that 😂 I'll be adding iOS 15 updates soon

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

      @@SwiftfulThinking haha, sorry for repeating ;)

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

    Wouldn’t async await be easier? I can’t see any tutorials you’ve made for it? I assume you made this before swift 5.5?

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

      Ignore, I found your other videos on swift concurrency lol 😅

  • @accountforyoutube2280
    @accountforyoutube2280 2 місяці тому

    Thank you so much master Nick 🥷🏻