Coder Social home page Coder Social logo

Question about caching about moa HOT 13 OPEN

evgenyneu avatar evgenyneu commented on August 28, 2024
Question about caching

from moa.

Comments (13)

evgenyneu avatar evgenyneu commented on August 28, 2024 1

Hello @kiwo12345, thank you for very good questions.

1) Does this lib cache images by default?

The answer is not simply yes or no, it is more subtle. By default, moa uses the caching strategy from the HTTP response. If caching is configured on your server then, yes, moa will cache the images accordingly.

// Default setting
Moa.settings.cache.requestCachePolicy = .useProtocolCachePolicy

2) Must I change some settings in Moa?

If you want to always cache images and ignore their HTTP caching policy, please use the returnCacheDataElseLoad setting. This can be used if one does not have control over the server caching settings.

Moa.settings.cache.requestCachePolicy = .returnCacheDataElseLoad

3) This might not belong here but does Moa clear the cache once it get full?

Moa uses URLSession caching built into the OS. If I remember correctly, the oldest images are automatically removed from the cache once it gets full to make some room for the new ones.

4) I am used to Kingfisher where most of the stuff is configured out of the box, cache clear, cache time etc

This is wonderful, Kingfisher is one of the libraries I recommend as a good alternative to moa.

5) Caching manual

If you need more information about caching in moa, please refer to the caching manual.

Let me know how it goes.

from moa.

kiwo12345 avatar kiwo12345 commented on August 28, 2024

@evgenyneu thanks for your answer! And sorry, I had two tabs open and thought I posted my question in the Auk lib.
I will then use Moa.settings.cache.requestCachePolicy = .returnCacheDataElseLoad in Auk :)

from moa.

evgenyneu avatar evgenyneu commented on August 28, 2024

No worries, I will leave this issue open. I think it is a very good question and it will be useful to others.

from moa.

kiwo12345 avatar kiwo12345 commented on August 28, 2024

@evgenyneu +1 for this tip you left in another issue: Moa.settings.cache.diskPath = "MyAppSharedCache" this way I can make the two libs use the same cache directory :)

from moa.

evgenyneu avatar evgenyneu commented on August 28, 2024

I am glad it was helpful. Let me know if you see any issue, so we can make the library better.

from moa.

yarodevuci avatar yarodevuci commented on August 28, 2024

do I set all this in viewDidLoad? @kiwo12345 @evgenyneu

from moa.

evgenyneu avatar evgenyneu commented on August 28, 2024

@yarodevuci, thanks for the question. The caching settings can be changed at any point in your program, including viewDidLoad. These settings will apply to all subsequent image loads.

from moa.

yarodevuci avatar yarodevuci commented on August 28, 2024

@evgenyneu Thanks for quick response
@evgenyneu your library does not work for me. Unfortunate
My downloads are getting cancelled all the time. What should I do?

Here is my code:

let moa = Moa()             
moa.onSuccess = { image in
     // image is loaded
     mediaMessage.image = image
     DispatchQueue.main.async(execute: {
          self.tableView.reloadData()
     })
     return image
}
moa.url = sendedImageURL

Console output:

[moa] 2017-05-06 07:15:51.210 GET https://**
[moa] 2017-05-06 07:15:51.212 Cancelled https://**

Image never gets loaded..:(

from moa.

evgenyneu avatar evgenyneu commented on August 28, 2024

@yarodevuci, the image download might be cancelled if another download is started. This can happen, for example, if you execute moa.url = sendedImageURL twice for the same Moa object. I would suggest placing a breakpoint at this line and see when it is called.

from moa.

evgenyneu avatar evgenyneu commented on August 28, 2024

Just curious, is it possible for you to use the moa property of the image?

mediaMessage.image.moa.url = "https://..."

If that's possible, that would make the program much simpler. :)

from moa.

yarodevuci avatar yarodevuci commented on August 28, 2024

@evgenyneu
Well, funny thing that I even tried put this in viewdidload


let moa = Moa()             
moa.onSuccess = { image in
     return image
}
moa.url = "https://..."

and same result.

from moa.

evgenyneu avatar evgenyneu commented on August 28, 2024

Oh, I probably know what it is :). The Moa object that you initialized in the function gets destroyed when the function exits. That cancels the download. I would suggest making let moa = Moa() an instance variable in the class instead.

from moa.

yarodevuci avatar yarodevuci commented on August 28, 2024

@evgenyneu yep, that did the trick! Thanks

from moa.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.