Scott MacRitchie
Scott MacRitchie

Reputation: 21

Akavache GetAndFetchLatest never fetches latest if a cached value exists

I'm trying to use the Akavache GetAndFetchLatest method in Xamarin.Forms project but it's not working as I expected and was hoping someone would be able to spot where I'm going wrong.

I'm calling the method like this:

BlobCache.Secure.GetAndFetchLatest (cacheId, FetchOrganizations, dt => true);

The first time my app starts, the FetchOrganizations method is called as expected and I can see my local server handling the request. However, as soon as Akavache has cached data, it only returns the cache and never calls the FetchOrganizations method again. My server never sees another request. If I use a short timeout, the FetchOrganizations method will be called when the cache expires. I've tried using the fetchPredicate parameter as you can see, but that doesn't seem to help.

Is there another way I can make GetAndFetchLatest always try to get fresh data? Am I misunderstanding how the fetchPredicate works?

Upvotes: 0

Views: 2168

Answers (1)

drewfrisk
drewfrisk

Reputation: 461

I realize this is old, but I figured it would be useful to answer in case anyone else comes across it. This blog post can be referenced for a more detail description in accomplishing the OP: http://www.maiersoft.de/blog/caching-httprequests-in-xamarin-crossplatform-app-using-akavache/

var cachedOrgs = BlobCache.Secure.GetAndFetchLatest (cacheId, FetchOrganizations, dt => true);

cachedOrgs.Subscribe(organizations => {
// do something with your results from the cache (first time)
// then do something with results from the server (second time)
});

The action passed to Subscribe() will get executed twice, once with the cached values, and a second with the values from your server.

Upvotes: 5

Related Questions