Akavache GetAndFetchLatest never fetches latest if a cached value exists

Hi All,
I'm trying to use the Akavache GetAndFetchLatest method 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?

Thanks,
Scott

Posts

  • KorayemKorayem EGMember ✭✭

    Been struggling myself with the same issue till I found the following code snippet

    public async Task<IEnumerable<WordPressPost>> GetPosts () {
    
        IEnumerable<WordPressPost> result = null;
        var cache = BlobCache.LocalMachine;
        var cachedPostsPromise = cache.GetAndFetchLatest(
            "posts", 
            () => GetPostsRemote(),
            offset =>
            {
                TimeSpan elapsed = DateTimeOffset.Now - offset;
                return elapsed > new TimeSpan(hours: 0, minutes: 10, seconds: 0);
            });
    
                cachedPostsPromise.Subscribe ( subscribedPosts => {
                    Debug.WriteLine("Subscribed Posts ready");
                    result=subscribedPosts;
                });
    
        result = await cachedPostsPromise.FirstOrDefaultAsync();
        return result;
    }
    

    For more info, read this article

  • JuanJChiwJuanJChiw ESMember ✭✭

    I think that even the code above returning Task is working, it should be "better" returning IObservable<IEnumerable<WordPressPost>> and subscribe to it, so you can get the cache result first and then the latest data.

    public Observable<IEnumerable<WordPressPost>> GetPosts () {
    
        var cache = BlobCache.LocalMachine;
        var cachedPostsPromise = cache.GetAndFetchLatest(
            "posts", 
            () => GetPostsRemote(),
            offset =>
            {
                TimeSpan elapsed = DateTimeOffset.Now - offset;
                return elapsed > new TimeSpan(hours: 0, minutes: 10, seconds: 0);
            });
    
                cachedPostsPromise.Subscribe ( subscribedPosts => {
                    Debug.WriteLine("Subscribed Posts ready");
                    result=subscribedPosts;
                });
        return cachedPostsPromise;
    }
    

    And in your ViewModel

    //All this code is based in ReactiveUI homepage
       public ReactiveCommand<IEnumerable<WordPressPost>> Search { get; protected set; }
       //I'm using ObservableCollection because https://github.com/reactiveui/ReactiveUI/issues/806
       public ObservableCollection<WordPressPostView> SearchResults { get; set; } = new ObservableCollection<WordPressPostView>();
        private void SetupCommand()
        {
                Search = ReactiveCommand.CreateAsyncObservable(_ =>GetPosts());
    
                // ReactiveCommands are themselves IObservables, whose value are the results
                // from the async method, guaranteed to arrive on the UI thread. We're going
                // to take the list of search results that the background operation loaded,
                // and them into our SearchResults.
                Search.Subscribe(results =>
                {
                //here you will have to play with the results if you need to map it to another class
                    var postsView = AutoMapper.Mapper.Map<List<WordPressPostview>>(results);
                    SearchResults.Clear();
            //this is an extension method https://github.com/pierceboggan/Moments/blob/master/Moments%20-%20CSharp/Moments.Shared/Helpers/ObservableExtensions.cs
                    SearchResults.AddRange(postsView);
                });
        }
    
  • batmacibatmaci DEMember ✭✭✭✭✭

    @JuanJChiw what does reactiveUI do better than ObservableCollections? does GetAndFetchLatest returning instantly if there is a cache and updating cache from the server on another background thread?

Sign In or Register to comment.