Parse-Swift icon indicating copy to clipboard operation
Parse-Swift copied to clipboard

Enable queries to use caching policies

Open aliasad106 opened this issue 3 years ago • 6 comments
trafficstars

New Feature / Enhancement Checklist

Current Limitation

The query does not allow me to add cachePolicy cacheThenNetwork as it was implemented there in the Parse iOS/macOS SDK.

Feature / Enhancement Description

When given the .cacheThenNetwork. It will first check from cache and respond and then it will load from network.

Example Use Case

Example use case is. When I load my feeds it first shows from cache and simultaneously fetches from network and respond and it also updates the cache with new data.

Alternatives / Workarounds

No

3rd Party References

Parse

aliasad106 avatar Jul 13 '22 21:07 aliasad106

Thanks for opening this issue!

  • 🎉 We are excited about your ideas for improvement!

Please complete the issue template in it's entirety by adding information under each header.

Caching was added in #196. You can change the default cache policy the SDK uses for your app when you initialize the SDK using requestCachePolicy, cacheMemoryCapacity, and cacheDiskCapacity as stated in the documentation. Details can be found by looking at Apple's documentation. The SDK defaults to Apple's default, useProtocolCachePolicy

If you want to choose a different caching policy for an individual query, simply pass the caching policy option you want to your query:

let options: API.Options = [.cachepolicy(returnCacheDataDontLoad)]
let query = GameScore.query("points" > 50)
let results = try await query.find(options: options)
...

cbaker6 avatar Jul 13 '22 22:07 cbaker6

@cbaker6 The apple documentation doesn't really guide how can we achieve cacheThenNetwork. And how can we update our cache with the updated response of query. P.S. How do we use useProtocolCachePolicy with combination to Parse query ?

aliasad106 avatar Jul 14 '22 08:07 aliasad106

P.S. How do we use useProtocolCachePolicy with combination to Parse query?

I’ve answered this in the previous comment, it’s automatically used for every query:

The SDK defaults to Apple's default, useProtocolCachePolicy

cbaker6 avatar Jul 14 '22 10:07 cbaker6

@cbaker6 But I don't see the cached response, It always fetched from network when we use useProtocolCachePolicy

aliasad106 avatar Jul 14 '22 13:07 aliasad106

This is probably because the SDK uses .POST instead of .GET for queries: https://github.com/parse-community/Parse-Swift/blob/b0bd3510100f17b2c2b848f4bd6aa189b268ee30/Sources/ParseSwift/Types/Query.swift#L1271-L1447

cbaker6 avatar Jul 14 '22 13:07 cbaker6

@cbaker6 I have updated to 4.9.3 and it still doesn't show me cacheThenNetwork. And by default it always fetches from network.

aliasad106 avatar Sep 02 '22 15:09 aliasad106