0
votes

I am trying to make offline sync to table from azure mobile service. My Xamarin Form version is 1.4.2.6359.

I try to test my code in OnCreate method of MainActivity. All preparation steps such as MobileServiceClient initialization, MobileServiceSQLiteStore initialization, SyncTable creation, etc are ok.

When I try to call PullAsync, I am getting NullReferenceException. I capture the package using Package Capture App from mobile. The request goes to Azure Mobile service and it returns the correct json data successfully.

When I try the same code in Xamarin Android project (not Xamarin Form), it is working fine.

To reproduce the issue. Just create Xamarin Form (Portable) project and use my code.

My Code

private async Task Test() {
    const string applicationURL = @"https://xxxx.azure-mobile.net/";
    const string applicationKey = @"xxxx";  

    CurrentPlatform.Init();

    var client = new MobileServiceClient(applicationURL, applicationKey);

    string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "store.db");

    if (!File.Exists(path)) {
        File.Create(path).Dispose();
    }

    var store = new MobileServiceSQLiteStore(path);
    store.DefineTable<Product>();

    await client.SyncContext.InitializeAsync(store);

    var productTable = client.GetSyncTable<Product>();

    try {
        await client.SyncContext.PushAsync();
        await productTable.PullAsync("allProducts", productTable.CreateQuery());
        var t = await productTable.ToListAsync();
        Console.WriteLine("Product Count : " + t.Count);        
    }
    catch (Java.Net.MalformedURLException ex) {
        Console.WriteLine(ex.Message);
    }
    catch (Exception e) {
        Console.WriteLine(e.Message);
    }
}

References:
http://azure.microsoft.com/en-us/documentation/articles/mobile-services-xamarin-android-get-started-offline-data/
http://blogs.msdn.com/b/carlosfigueira/archive/2014/04/07/deep-dive-on-the-offline-support-in-the-azure-mobile-service-managed-client-sdk.aspx

1

1 Answers

0
votes

I got the solution for this case.
As far as my understanding, this is what is happening. During the application is loading, I call PullAsync. It is async call and during this call, application keeps loading other components. The actual NullReferenceException is coming from OnPrepareOptionsMenu function (Xamarin.Forms.Platform.Android.AndroidActivity.OnPrepareOptionsMenu). The exception is happening on other thread and the thread simply dies. That's why I cannot get stack trace from my main thread.
This NullReferenceException issue is totally not related to Azure Mobile Service.

I override OnPrepareOptionsMenu in MainActivity and add try-catch block to base class function call. The problem is solved. Here is my code in MainActivity class.

public override bool OnPrepareOptionsMenu(IMenu menu) {
    try {
        // I am always getting menu.HasVisibleItems = false in my app
        if (menu != null && menu.HasVisibleItems) {
            // Exception is happening when the following code is executed
            var result = base.OnPrepareOptionsMenu(menu);
            return result;
        }
    }
    catch{      
    }
    return true;
}

I don't really understand why it is happening. Please point me out if you have better understanding of this case and better solution.

I think my issue is same as this : http://forums.xamarin.com/discussion/23579/exception-whilte-trying-to-open-activity-from-xamarin-forms-page