moa icon indicating copy to clipboard operation
moa copied to clipboard

Question about caching

Open kiwo12345 opened this issue 8 years ago • 13 comments

Thanks for sharing this lib!

Does this lib cache images by default? Or must I change some settings in Moa?

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

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

kiwo12345 avatar Oct 24 '16 16:10 kiwo12345

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.

evgenyneu avatar Oct 24 '16 19:10 evgenyneu

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

kiwo12345 avatar Oct 24 '16 19:10 kiwo12345

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

evgenyneu avatar Oct 24 '16 19:10 evgenyneu

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

kiwo12345 avatar Oct 24 '16 19:10 kiwo12345

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

evgenyneu avatar Oct 24 '16 23:10 evgenyneu

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

yarodevuci avatar May 06 '17 07:05 yarodevuci

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

evgenyneu avatar May 06 '17 07:05 evgenyneu

@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..:(

yarodevuci avatar May 06 '17 07:05 yarodevuci

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

evgenyneu avatar May 06 '17 08:05 evgenyneu

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. :)

evgenyneu avatar May 06 '17 08:05 evgenyneu

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

yarodevuci avatar May 06 '17 08:05 yarodevuci

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.

evgenyneu avatar May 06 '17 09:05 evgenyneu

@evgenyneu yep, that did the trick! Thanks

yarodevuci avatar May 06 '17 09:05 yarodevuci