Skip to content

Paywalls: explicitly cache images for paywalls #3477

Closed
aboedo wants to merge 5 commits into
mainfrom
andy/image_caching
Closed

Paywalls: explicitly cache images for paywalls #3477
aboedo wants to merge 5 commits into
mainfrom
andy/image_caching

Conversation

@aboedo

@aboedo aboedo commented Nov 28, 2023

Copy link
Copy Markdown
Member

This adds explicit caching to images for Paywalls.

In my testing we seem to be mostly re-fetching images on every load. We're studying also improving the cache headers and that might (hopefully) render this PR unnecessary, but opening this up in the meantime just in case.

This PR fixes the re-fetching as far as I can tell.

@aboedo aboedo self-assigned this Nov 28, 2023
@aboedo aboedo requested a review from a team November 29, 2023 21:24
@aboedo aboedo marked this pull request as ready for review November 29, 2023 21:24
@aboedo

aboedo commented Nov 29, 2023

Copy link
Copy Markdown
Member Author

this is ready to go from a code perspective... but I don't love the idea of not having any tests in it?

Looks like our setup is mostly snapshot testing, which makes sense, but for stuff that is Paywalls-side logic it'd be great to have a test target. I'll make that as a separate PR if I get the time

@aboedo

aboedo commented Nov 29, 2023

Copy link
Copy Markdown
Member Author
image uhh wat

@aboedo

aboedo commented Nov 29, 2023

Copy link
Copy Markdown
Member Author

nevermind we do have tests and I even modified test cases earlier today, what am I saying

@aboedo

aboedo commented Nov 29, 2023

Copy link
Copy Markdown
Member Author

going back to draft to write tests for it

@aboedo aboedo marked this pull request as draft November 29, 2023 21:56
Comment thread RevenueCatUI/Views/RemoteImage.swift Outdated
struct RemoteImage: View {

@StateObject private var loader: ImageLoader
private let cache: URLCache

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small thing but do we need to store this here? Seems like this is unused.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

@aboedo

aboedo commented Dec 4, 2023

Copy link
Copy Markdown
Member Author

Update here: we're actually supposed to be caching this stuff here https://github.com/RevenueCat/purchases-ios/blob/main/Sources/Paywalls/PaywallCacheWarming.swift, so we should dig into why that's not happening correctly before merging this, and unify the caching for both

@aboedo

aboedo commented Dec 4, 2023

Copy link
Copy Markdown
Member Author

PaywallCacheWarming uses a different instance of URLCache, so we'll have to unify them. Also, we're using the shared cache, which isn't great because we're also setting the actual values for the cache in the whole app. A bit of a bigger refactor, though, since the warming happens in RC and the image loading currently happens in UI

@aboedo aboedo force-pushed the andy/image_caching branch from 033b44c to 245cd29 Compare December 4, 2023 21:11

@NachoSoto NachoSoto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that it means moving away from AsyncImage, but I guess it's time to finally implement this manually and make it work consistently.


@Published var image: UIImage?
@Published var error: Error?
private var cancellable: AnyCancellable?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're only storing one of these, but you can call load multiple times, so only the last one is cancelled?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update this so that overriding a cancellable cancels the previous one.

Comment on lines +23 to +24
@Published var image: UIImage?
@Published var error: Error?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a Result<UIImage, Error> instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that'd make a lot more sense

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension URLCache {

static let imageCache = URLCache(memoryCapacity: 5 * 1000 * 1000, diskCapacity: 30 * 1000 * 1000)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed! I commented the same thing

NachoSoto added a commit that referenced this pull request Dec 7, 2023
Supersedes #3477.

This replaces `AsyncImage` with a custom implementation that allows us to ensure caching works reliably by explicitly using a custom `URLSession` with a shared `URLCache`.
@NachoSoto

Copy link
Copy Markdown
Contributor

Replaced by #3498

@NachoSoto NachoSoto closed this Dec 8, 2023
@NachoSoto NachoSoto deleted the andy/image_caching branch December 8, 2023 01:36
NachoSoto added a commit that referenced this pull request Dec 8, 2023
Supersedes #3477.

This replaces `AsyncImage` with a custom implementation that allows us to ensure caching works reliably by explicitly using a custom `URLSession` with a shared `URLCache`.
NachoSoto added a commit that referenced this pull request Dec 11, 2023
Supersedes #3477.

This replaces `AsyncImage` with a custom implementation that allows us to ensure caching works reliably by explicitly using a custom `URLSession` with a shared `URLCache`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants