Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about caching #12

Open
kiwo12345 opened this issue Oct 24, 2016 · 13 comments
Open

Question about caching #12

kiwo12345 opened this issue Oct 24, 2016 · 13 comments

Comments

@kiwo12345
Copy link

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

@evgenyneu
Copy link
Owner

evgenyneu commented Oct 24, 2016

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.

@kiwo12345
Copy link
Author

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

@evgenyneu
Copy link
Owner

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

@kiwo12345
Copy link
Author

kiwo12345 commented Oct 24, 2016

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

@evgenyneu
Copy link
Owner

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

@yarodevuci
Copy link

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

@evgenyneu
Copy link
Owner

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

@yarodevuci
Copy link

yarodevuci commented May 6, 2017

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

@evgenyneu
Copy link
Owner

evgenyneu commented May 6, 2017

@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
Copy link
Owner

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

@yarodevuci
Copy link

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

@evgenyneu
Copy link
Owner

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.

@yarodevuci
Copy link

@evgenyneu yep, that did the trick! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants