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

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ม.ค. 2025

ความคิดเห็น • 91

  • @hakamgo3894
    @hakamgo3894 10 หลายเดือนก่อน +2

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

  • @irvingchen7072
    @irvingchen7072 3 ปีที่แล้ว +9

    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  3 ปีที่แล้ว

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

  • @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 :)

  • @indnjcyoung
    @indnjcyoung 2 ปีที่แล้ว +2

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

  • @gurcangulec
    @gurcangulec ปีที่แล้ว +3

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

  • @danielcrompton7818
    @danielcrompton7818 3 หลายเดือนก่อน +1

    Just a heads up about a mistake I made, In my model `PostModel` I said **userID instead of userId**. Make sure it is a lower case d.

    • @ashleym6157
      @ashleym6157 หลายเดือนก่อน

      OMG!!! I can now see the downloaded data in the simulation/canvas👏

  • @hamidevtech
    @hamidevtech 2 ปีที่แล้ว +2

    Very thorough and concise. Completely understandable 👌.

  • @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 🙌🙌🙌

  • @Jump_Jay
    @Jump_Jay 2 ปีที่แล้ว +1

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

  • @RichardBaileyrichoncode
    @RichardBaileyrichoncode 5 หลายเดือนก่อน

    Great video. Canceleables set keeps growing.
    On sink completion, clear the set or remove the subscription from the set.

  • @gucp
    @gucp ปีที่แล้ว +4

    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.

  • @sarikapaghdal6150
    @sarikapaghdal6150 2 ปีที่แล้ว +1

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

  • @OrtoInScatola
    @OrtoInScatola ปีที่แล้ว +1

    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?

    • @lowlydeveloper3693
      @lowlydeveloper3693 2 หลายเดือนก่อน

      I was wondering exactly the same thing 🙂... I haven't gotten to that point yet, but I hope that Nick's Concurrency Tutorial might explain it: th-cam.com/play/PLwvDm4Vfkdphr2Dl4sY4rS9PLzPdyi8PM.html

  • @spradohak
    @spradohak ปีที่แล้ว

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

  • @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 3 ปีที่แล้ว

      @@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)

  • @user-a_fellow_traveler
    @user-a_fellow_traveler ปีที่แล้ว

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

  • @dglalperen
    @dglalperen 3 ปีที่แล้ว +2

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

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +3

      Firestore + Combine is a powerful combo. Coming soon 😎

  • @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!

  • @vazzhh
    @vazzhh 9 หลายเดือนก่อน

    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  9 หลายเดือนก่อน +1

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

  • @MohamedAli-kg5im
    @MohamedAli-kg5im 10 หลายเดือนก่อน

    Fantastic vidio, I am so grateful to you, Nick

  • @Bhaukaal-YT-001
    @Bhaukaal-YT-001 ปีที่แล้ว

    Very practical example thank you

  • @AZMerf
    @AZMerf 3 ปีที่แล้ว +1

    Still with you. You’re doing great.

  • @yasseraltamimi6171
    @yasseraltamimi6171 3 ปีที่แล้ว +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  3 ปีที่แล้ว +3

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

    • @toastrecon
      @toastrecon 3 ปีที่แล้ว

      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!

  • @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

  • @mustafasabahy3092
    @mustafasabahy3092 2 ปีที่แล้ว +1

    you make life easier

  • @rishabhbhatia1847
    @rishabhbhatia1847 15 วันที่ผ่านมา

    Do we need to learn Combine framework or Async/Await is enough as its easy and modern ?

  • @evgeshaky
    @evgeshaky 2 ปีที่แล้ว

    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)

  • @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!

  • @Nunny25
    @Nunny25 3 ปีที่แล้ว +1

    Amazing video, .. thanks

  • @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 2 ปีที่แล้ว

      Great question Jerry, glad you asked it

  • @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!

  • @ushadesai6306
    @ushadesai6306 3 ปีที่แล้ว +1

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

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +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 3 ปีที่แล้ว

      @@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

    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.

  • @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?

  • @dre5671
    @dre5671 2 ปีที่แล้ว +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  2 ปีที่แล้ว

      The JSONDecoder has a keyDecodingStrategy that you can set

    • @dre5671
      @dre5671 2 ปีที่แล้ว

      @@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  2 ปีที่แล้ว +1

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

  • @peterellison8306
    @peterellison8306 3 ปีที่แล้ว +1

    How do we cancel the publisher if we want to?

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +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.

  • @zhong2341
    @zhong2341 3 ปีที่แล้ว +1

    thank you so much!

  • @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.

  • @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.

  • @BlackLightning0Games
    @BlackLightning0Games 2 ปีที่แล้ว

    Can you please make a tutorial on Post Requests

  • @Wusagi-gi5xk
    @Wusagi-gi5xk 11 หลายเดือนก่อน

    Thank you so much...

  • @jamesbush7717
    @jamesbush7717 ปีที่แล้ว +1

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

    • @ericbarnes6362
      @ericbarnes6362 9 หลายเดือนก่อน

      you just solved a bug in my brain. Thanks

  • @kal5211
    @kal5211 ปีที่แล้ว +1

    My man.

  • @pasha7324
    @pasha7324 ปีที่แล้ว

    👏👏👏👏👏

  • @williamwildsmith5162
    @williamwildsmith5162 2 ปีที่แล้ว

    All double-dutch to me, this one!

  • @dugrut1325
    @dugrut1325 2 ปีที่แล้ว

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

  • @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 3 ปีที่แล้ว +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  3 ปีที่แล้ว +2

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

    • @alisultanabdullah6924
      @alisultanabdullah6924 3 ปีที่แล้ว

      @@SwiftfulThinking haha, sorry for repeating ;)

  • @accountforyoutube2280
    @accountforyoutube2280 8 หลายเดือนก่อน

    Thank you so much master Nick 🥷🏻