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

iOS cache not entirely cleared when calling clearAll() #6375

Closed
jvanderwee opened this issue Feb 3, 2025 · 10 comments
Closed

iOS cache not entirely cleared when calling clearAll() #6375

jvanderwee opened this issue Feb 3, 2025 · 10 comments
Milestone

Comments

@jvanderwee
Copy link
Contributor

jvanderwee commented Feb 3, 2025

Version

4.1.1

Summary

Two possible issues:

  • request is cached for mutations with .doNotStore(true)
  • request is still in cache after calling apolloClient.apolloStore.clearAll()

Steps to reproduce the behavior

  1. Call a mutation with .doNotStore(true), e.g.
apolloClient
    .mutation(FooMutation())
    .doNotStore(true)
    .execute()
  1. Grep ~/Library/Developer/CoreSimulator/Devices/<id>/data/Containers/Data/Application/<id>/Library/Caches/<id>/Cache.db-wal for "mutation"
  • mutation request is present in cache
  1. Call apolloClient.apolloStore.clearAll()
  2. Grep as above
  • mutation request still present in cache

Logs

(Your logs here)
@BoD
Copy link
Contributor

BoD commented Feb 3, 2025

Hi! I could not reproduce on my end, but a couple of remarks:

  • you mention that you're looking at the .db-wal file but you should be looking at the .db file.
  • you mention you see FooMutation in the cache, but we don't store references to the operation names anywhere - only the field names should be in there

You can check the contents of the db by opening it with the cache viewer of the Apollo IDE plugin, or with sqlite3 on the command line and by executing select * from records;

@BoD BoD added the ⌛ Waiting for info More information is required label Feb 3, 2025
@jvanderwee
Copy link
Contributor Author

Hey @BoD 👋 I think this might be the NSURLCache. Wondering if it would be possible to prevent the NSURLRequest from caching data if we set doNotStore(true)?

@jvanderwee
Copy link
Contributor Author

You're right about not seeing "FooMutation", but I do see the operation name. I've edited above

@BoD
Copy link
Contributor

BoD commented Feb 4, 2025

Oh I see - doNotStore() and clearAll() are actually about the normalized cache and are not related to the HTTP cache.

The HTTP responses should not be cached actually as we set a NSURLRequestReloadIgnoringCacheData policy on the requests here. Are you positive the responses are actually cached? (Maybe this can be confirmed by going to airplane mode, or by using a tool like Charles Proxy).

In general there's not really a reason to use both an HTTP cache and the normalized cache at the same time, the normalized cache being sufficient. I think passing NSURLSessionConfiguration.ephemeralSessionConfiguration() to the constructor of DefaultHttpEngine should disable any disk storage.

@jvanderwee
Copy link
Contributor Author

Yes they are definitely cached.

I think NSURLRequestReloadIgnoringCacheData means that data is not read from the cache, but data can still be cached - https://medium.com/swiftblade/urlrequest-can-be-cached-regardless-of-cachepolicy-f33b25d96c63

I wonder if nil could be passed to the willCacheResponse completion handler here?

@BoD
Copy link
Contributor

BoD commented Feb 4, 2025

Thanks for checking! That makes sense.

Can you try ephemeralSessionConfiguration()?

Something like

val client = ApolloClient.Builder()
    .httpEngine(
      DefaultHttpEngine(nsUrlSessionConfiguration = NSURLSessionConfiguration.ephemeralSessionConfiguration())
    )
   (...)
  .build()

If it works we may want to use that by default, as there's no reason to write to a cache that is never read.

@jvanderwee
Copy link
Contributor Author

no sign of the mutation in the cache after making that change 🙌

@BoD
Copy link
Contributor

BoD commented Feb 4, 2025

Thanks for checking! Do you want to open a PR to use that by default?

@BoD
Copy link
Contributor

BoD commented Feb 4, 2025

Thanks for the PR, I just merged it! I'll close this issue then.

@BoD BoD closed this as completed Feb 4, 2025
Copy link
Contributor

github-actions bot commented Feb 4, 2025

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Kotlin usage and allow us to serve you better.

@BoD BoD removed the ⌛ Waiting for info More information is required label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants