A better AsyncImage with cache in SwiftUI

Поделиться
HTML-код
  • Опубликовано: 13 окт 2024

Комментарии • 45

  • @FloWritesCode
    @FloWritesCode 3 года назад +1

    You already caught me with the thumbnail haha. Great video!

  • @chorbogturongi_masjidi
    @chorbogturongi_masjidi 3 месяца назад

    thank you very much im using your cachedSyncImage into my project effectively

  • @maik_4
    @maik_4 3 месяца назад

    Hi Pitt! Thank you for this elegant and simple solution 👏🏻However, sometimes images are now only partially displayed (the rest is gray).
    AsyncImage might call its content closure with AsyncImagePhase == success with the image only partially loaded when the view disappears and the download task is canceled (mostly happens in a list when scrolling).

  • @TheGioGiol
    @TheGioGiol Год назад

    Amazing video! I really like the way you explained this! Would like to see more on persistent Image cache, but I guess that'll be for another video

    • @swiftandtips
      @swiftandtips  Год назад +1

      Thanks for your comment! I would like to bring that topic later on! 😁

    • @TheGioGiol
      @TheGioGiol Год назад

      @@swiftandtips Awesome!!!

  • @LQ_LQ_LQ
    @LQ_LQ_LQ Год назад

    So elegant! Loved the video, learned a lot. Thanks a ton and keep it up :)

  • @erdemsenol9642
    @erdemsenol9642 3 года назад +1

    Great Video, but 2 things bug my mind. 1-) why the app is taking so much memory? 2-) If we are going to use a search function in the list scroll paging method is not suitable. why not use a for loop?
    for i in 1...page{
    let characters = try await service.fetchCharacters(page: i)
    self.characters.append(contentsOf: characters
    }
    so that we can search all characters.

    • @swiftandtips
      @swiftandtips  3 года назад +1

      Hi @erdem, for 1) we need to implement a LRU cache to optimize the memory. Check out my newsletter: www.getrevue.co/profile/swiftandtips/issues/swift-tips-issue-1-september-2021-733761
      Not sure about 2), yeah, would be cool for searching in that screen, good update 👍.
      Thanks for sharing 😀

  • @ashleydube3710
    @ashleydube3710 2 года назад

    You just got a new subscriber, thanx😉

  • @박박박-p5t1w
    @박박박-p5t1w 6 месяцев назад

    good good

  • @creat-ivo
    @creat-ivo 2 года назад

    Thank you it worked perfect for me

  • @oleg991
    @oleg991 2 года назад

    Hi Pitt! Thank you for the video, it worked great for me! The only thing I changed was the type of URL - I made it optional. I did not want to use Kingfisher for my small pet-project, and your solution is perfect. You could even make a Swift Package with your CacheAsyncImage, or I can do that if you don't mind.

    • @swiftandtips
      @swiftandtips  2 года назад

      Hi Oleg, thank you for your idea! Actually I wanted to make a video about Swift Packages. 👌😀
      I’m glad you found this code useful ☺️

    • @oleg991
      @oleg991 2 года назад

      ​@@swiftandtips Hello again. Guess what? I found a bug :) When I use your CacheAsyncImage and attach a .task modifier to the view with this image I get error: Error Domain=NSURLErrorDomain Code=-999. It happened because CacheAsyncImage leads to extra requests to the server and the first one is trying to get cancelled.
      I spent 3 hours trying to fix that until I just replaced CacheAsyncImage with AsyncImage.
      Now I do not have cache, but do not send extra requests to the server

    • @swiftandtips
      @swiftandtips  2 года назад

      My apologies for the inconvenience 😅. I will take a look to find the issue. Could you share a piece of code calling CacheAsyncImage? I would like to get the right idea on why this is happening. Thank you 😄

  • @LQ_LQ_LQ
    @LQ_LQ_LQ Год назад

    Do you think you could show us/me how to additionally add the other initializer of AsyncImage for this? (The one with the placeholder).
    I tried to add it to the existing code, but I'm running into several issues. Would really love to see it!

    • @swiftandtips
      @swiftandtips  Год назад

      Hi @tehLilaQ, I would take back the work of that later adding more initializers and improving the cache system.
      What is your problem right now? Any compile issue?

    • @LQ_LQ_LQ
      @LQ_LQ_LQ Год назад

      @@swiftandtips Yea, it's telling me `cannot convert value of type 'some View' to closure result type 'Text'`, when I have e.g. the Image in the content closure, and a Text in the placeholder closure. Not sure where I'm going wrong :(

    • @swiftandtips
      @swiftandtips  Год назад

      @@LQ_LQ_LQ I could help you if you share a gist link with your code and see the actual problem.

  • @madmandu
    @madmandu 3 года назад

    Hey there! I just saw your video on the base model Macbook Air M1 for coding and I wanted to ask you how it is holding up? I too, thought about getting a Pro with all the upgrades, but wanted to see how you are doing with your Air.
    Thank you!

    • @swiftandtips
      @swiftandtips  3 года назад +1

      Hi @Steven, answering your question, Macbook Air M1 still awesome! I work with this machine daily to code and generate my videos and I don't have any problem in terms of performance. Although, 256gb are not enough for content creation to be honest, if you can spend more on capacity is better than ram.
      However, Apple events are really close, I will suggest to wait until see the announcements before buying the right mac for you :)

    • @madmandu
      @madmandu 3 года назад

      @@swiftandtips Thank you so much for taking the time to respond! Take care, sir! 💪🏻

  • @ryanchannel_HK
    @ryanchannel_HK 2 года назад

    Thanks for this great video! May I know how long does these images kept in cache for?

    • @swiftandtips
      @swiftandtips  2 года назад

      Hi Ryan, the cache has room for improvements. Right now is pretty basic and images will stay in memory the whole execution.
      Thanks for the support!

    • @ryanchannel_HK
      @ryanchannel_HK 2 года назад

      @@swiftandtips wondering how can I remove it say after one week?

    • @swiftandtips
      @swiftandtips  2 года назад +1

      @@ryanchannel_HK Do you mean persisting the data for 1 week?

    • @SebastianIrazabal
      @SebastianIrazabal 2 года назад

      ​@@swiftandtips First of all, nice video and explanation. Keep on that way! I have the same question. Should I store it with CoreData or there's a more elegant way? I'm new at SwiftUI

  • @0xjorgev
    @0xjorgev 2 года назад

    really helpful tips!!

  • @gelferstone5692
    @gelferstone5692 3 года назад

    Awesome tutorial!

  • @jeffdickson8982
    @jeffdickson8982 3 года назад

    Great video... can you please make a video on making request from user input with Combine

    • @swiftandtips
      @swiftandtips  3 года назад +1

      Thank you Jeff! 😃
      I have a video about the basics of Combine: ruclips.net/video/IJinuMLaERE/видео.html
      Let me know if it helps you.

  • @halahmilksheikh
    @halahmilksheikh 3 года назад

    How often does it get emptied out? If you force close the app, do you still have the images?

    • @swiftandtips
      @swiftandtips  3 года назад +1

      Hi @Halal, thank you for your comment. If you are talking about CacheAsyncImage, everything is in-memory. We will have to support persistency later. For AsyncImage the same, it doesn’t save anything at all. Is that answering your questions?

    • @halahmilksheikh
      @halahmilksheikh 3 года назад

      @@swiftandtips Thank you Mr. Tips, that answers my question! Great app by the way.

  • @indiekiduk
    @indiekiduk 2 года назад

    Is there a reason you didn't simply use the built-in URLCache with the shared URLSession that AsyncImage uses?

    • @swiftandtips
      @swiftandtips  2 года назад

      Hi indiekiduk, that’s the point of the video, it doesn’t work for AsyncImage yet 😅

    • @indiekiduk
      @indiekiduk 2 года назад

      Hi just make an UIApplicationDelegateAdaptor and put the swift equivalent of this in your didFinishLaunching: NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity: * * diskCapacity: * * diskPath:nil];
      [NSURLCache setSharedURLCache:URLCache];

    • @swiftandtips
      @swiftandtips  2 года назад

      Interesting, I will try that. Thank you!!

    • @vincenzoflaminio4340
      @vincenzoflaminio4340 Год назад

      @@indiekiduk Hi I've changed both URLCache.shared.memoryCapacity = 1024 * 1024 * 512
      URLCache.shared.diskCapacity = 1024 * 1024 * 512
      anyway still images are not caching so it's not due to memory capacity that is changed (I can see it with breakpoints)

  • @AshokaSuyash
    @AshokaSuyash 8 месяцев назад

    Is there a way to cache my AsyncImage before it appears on screen?

    • @swiftandtips
      @swiftandtips  8 месяцев назад

      Maybe using a regular Image.