Akavache总是调用fetchFunc

时间:2016-11-07 21:33:03

标签: caching xamarin.forms akavache

我在Xamarin应用程序中使用以下代码使用Akavache,并且它的行为方式与我认为应该的方式不同。可能是我对它应该如何的误解,但这让我发疯了。

所以在我的viewmodel中,我正在调用FetchNewsCategories并为该项指定5分钟的缓存。我期望发生的是,如果缓存项不存在,它将调用fetchFunc(即FetchNewsCategoriesAsync),但如果我在缓存超时5分钟内调用该服务任意次,它应该只是给我缓存的项目,而不是服务器调用。在我尝试过的所有情况下,它都会继续进行休息调用,并且永远不会给我缓存的项目。我也尝试使用GetAndFetchLatest,如果有缓存项,它不会进行其余调用,但它也不会在viewmodel中的subscribe事件中进行调用。 我在这里做错了什么想法?

编辑:我在Android(Nexus 5 KitKat API19)上测试了相同的代码,它运行完美。我将重置我的IOS模拟器,看看是否有什么东西不合适。

NewsService.cs

public static async Task<ServiceResponse<List<ArticleCategoryInfo>>> FetchNewsCategoriesAsync(BlogSourceType blogSource)
    {
        return await ServiceClient.POST<List<ArticleCategoryInfo>>(Config.ApiUrl + "news/categories", new
        {
            ModuleId = Int32.Parse(Config.Values[blogSource == BlogSourceType.News ? ConfigKeys.KEY_NEWS_MODULE_ID : ConfigKeys.KEY_BLOG_MODULE_ID])
        });
    }

public static IObservable<ServiceResponse<List<ArticleCategoryInfo>>> FetchNewsCategories(BlogSourceType blogSource)
    {
        var cache = BlobCache.LocalMachine;
        var cachedCategories = cache.GetOrFetchObject("categories" + blogSource,
                    async () => await FetchNewsCategoriesAsync(blogSource),
                    DateTime.Now.AddMinutes(5));

        return cachedCategories;
    }

NewsViewModel.cs

public async Task LoadCategories()
{
var cachedCategories = NewsService.FetchNewsCategories(blogSource);

cachedCategories.Subscribe((obj) => { Device.BeginInvokeOnMainThread(() => DisplayCategories(obj.Result,"Subscribe"));});
return;
}

private void DisplayCategories(IList<ArticleCategoryInfo> categories, string source)
    {
        Categories.Clear();
        System.Diagnostics.Debug.WriteLine("Redisplaying categories from " + source);
        foreach (var item in categories)
        {
            Categories.Add(item);
        }
    }

1 个答案:

答案 0 :(得分:0)

只是想将我的解决方案添加到我上面遇到的问题中,以便参考有这个问题的其他人。

我试图缓存的ServiceResponse对象中有一个HttpResponseMessage我怀疑是导致序列化错误,可能是一个循环引用,所以它从来没有被缓存并最终调用端点每次。我最后在该属性上放了一个[IgnoreDataMemberAttribute],因此没有序列化,问题就消失了。

我最终以下列方式处理订阅以处理错误并确保绑定到IsBusy属性的活动指示符已正确更新。

public async Task LoadActivities(bool refresh)
    {

        IsBusy = true;

        if (refresh) OlderThanJournalId = int.MaxValue;

        var cached = ActivityService.FetchJournalItems(GroupId, OlderThanJournalId, refresh);

        cached.Subscribe((result) => { Device.BeginInvokeOnMainThread(() => { 
            DisplayActivities(result); 
        }); }, (err) => HandleError(err), () => IsBusy = false);

    }

public void HandleError(Exception ex) {
        IsBusy = false;
        DialogService.ShowErrorToast(AppResources.ErrorMessage, "Unable to load activity stream.");
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }

private void DisplayActivities(ServiceResponse<List<JournalItem>> response)
    {
        if (!response.IsConnected) {
            DialogService.ShowInfoToast(AppResources.ErrorMessage, AppResources.NotConnected);
            return;
        }
        if (!response.Authorized) {
            App.LoginManager.Logout();
        }
        Activities.Clear();
        foreach (var item in response.Result)
        {
            Activities.Add(item);
        }
    }

BeginInvokeOnMainThread用于确保在UI中看到DisplayActivities中ObservableCollection的更新。