Inrego
Inrego

Reputation: 1533

First view not shown properly when Akavache is used before

If I'm fetching an object from cache before showing my first view, then the app will just show a blank white page instead (on Android, didn't test other platforms). If I lock the screen and unlock it, then my view shows. It's quite simple to reproduce:

Here is a sample App.xaml.cs which will reproduce the problem.

using System.Reactive.Linq;
using Akavache;
using Xamarin.Forms;
using AkavacheTest.Services;
using AkavacheTest.Views;

namespace AkavacheTest
{
    public partial class App : Application
    {

        public App()
        {
            InitializeComponent();

            DependencyService.Register<MockDataStore>();
        }

        protected override async void OnStart()
        {
            Akavache.Registrations.Start("Test");
            var test = await BlobCache.LocalMachine.GetOrFetchObject("Settings", async () => "New");
            MainPage = new MainPage();
        }
    }
}

So what's going on here, and more importantly - is there a fix or workaround for this?

Upvotes: 0

Views: 139

Answers (1)

Inrego
Inrego

Reputation: 1533

As far as workarounds go, I managed to find one. If I set the MainPage before getting from cache, then I can set it again after grabbing cache without problem.

These both work:

    protected override async void OnStart()
    {
        MainPage = new Page();
        Akavache.Registrations.Start("Test");
        var test = await BlobCache.LocalMachine.GetOrFetchObject("Settings", async () => "New");
        MainPage = new MainPage();
    }

    protected override async void OnStart()
    {
        Akavache.Registrations.Start("Test");
        MainPage = new Page();
        var test = await BlobCache.LocalMachine.GetOrFetchObject("Settings", async () => "New");
        MainPage = new MainPage();
    }

I'm still curious why this is happening, though. See comment by @BenReierson.

With this information in mind, a better workaround would be this:

protected override void OnStart()
{
    Akavache.Registrations.Start("Test");
    var test = BlobCache.LocalMachine.GetOrFetchObject("Settings", async () => "New").GetAwaiter().GetResult();
    MainPage = new MainPage();
}

So we actually wait for the result from Akavache before setting MainPage. While the other workaround worked, it's a bit wasteful to set a page (which causes it to be rendered), just to set another page almost instantly after.

Upvotes: 1

Related Questions