parse-community / Parse-Swift

The Swift SDK for Parse Platform (iOS, macOS, watchOS, tvOS, Linux, Android, Windows)
https://parseplatform.org
MIT License
308 stars 69 forks source link

Enable queries to use caching policies #382

Closed aliasad106 closed 2 years ago

aliasad106 commented 2 years ago

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

parse-github-assistant[bot] commented 2 years ago

Thanks for opening this issue!

cbaker6 commented 2 years ago

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](https://parseplatform.org/Parse-Swift/release/documentation/parseswift/parseswift/initialize\(applicationid:clientkey:masterkey:serverurl:livequeryserverurl:allowingcustomobjectids:usingtransactions:usingequalqueryconstraint:keyvaluestore:requestcachepolicy:cachememorycapacity:cachediskcapacity:migratingfromobjcsdk:deleti-97083/). 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)
...
aliasad106 commented 2 years ago

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

cbaker6 commented 2 years ago

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

aliasad106 commented 2 years ago

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

cbaker6 commented 2 years ago

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

aliasad106 commented 2 years ago

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