apollographql / apollo-ios

📱  A strongly-typed, caching GraphQL client for iOS, written in Swift.
https://www.apollographql.com/docs/ios/
MIT License
3.89k stars 728 forks source link

clearCache for SQLiteNormalizedCache doesn't clear the sqlite file #3389

Closed avielg closed 5 months ago

avielg commented 5 months ago

Summary

We have a "clear cache" setting in our app, and when the user taps it we call apollo.store.clearCache(..) which succeeds, but the underlying sqlite file stays exactly the same size and is not removed. I tried to just delete the file after the success callback, but the store never re-creates it. Is this a bug with clearCache function or am I doing something wrong?

Thank you!

Version

1.10.0

Steps to reproduce the behavior

Create a store like so:

let fileURL = try FileManager.default
  .url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  .appendingPathComponent("cache.sqlite")
let cache = try SQLiteNormalizedCache(fileURL: fileURL)
let store = ApolloStore(cache: cache)
self.client = ApolloClient(networkTransport: transport, store: store)

Try to clear cache:

self.client.store.clearCache { result in
  // result is success but the sqlite file is not deleted or any smaller
}

Logs

No response

Anything else?

No response

calvincestari commented 5 months ago

Hi @avielg - I suspect the file size behaviour is due to the internals of sqlite; have you tried setting the shouldVacuumOnClear property in SQLiteNormalizedCache.init to true? By default it is false and when configured with true the vacuum command will be run whenever the sqlite cache is cleared. It seems that without forcing that behaviour sqlite is left to decide when to recover disk space which may not be immediately after deleting the records.

As for the file not being removed; we never remove the db file. It's created at startup if it doesn't exist but we have no code to delete the file.

avielg commented 5 months ago

oh shouldVacuumOnClear does it! Didn't know about this option, thank you! This is good to close from my end :)

github-actions[bot] commented 5 months ago

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