Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

XF + Azure Mobile App + Offline Sync

LimYeenFeiLimYeenFei MYMember ✭✭
edited January 2017 in Xamarin.Forms

Appreciate any help regarding issue in my XF + Azure Mobile App + Offline Sync.
Common Startup Flow A:
1.) Create azure service objects (MobileServiceClient)
2.) Create db store objects (MobileServiceSQLiteStore)
3.) Register entity to sqlite table via DefineTable()
4.) MobileServiceClient.SyncContext.InitializeAsync()
5.) Check for cached auth token

Branch Flow B : Invalid/No Token found (First launch)
6.) Display login page (XF)
7.) Perform custom authentication via MobileServiceClient.InvokeApiAsync()
8.) Extract GWT auth token from response (failed scenario is omitted here)
9.) Cache auth token for future usage (with token expiry info)

Branch Flow C : Valid Token found (Subsequent launch)
10.) Create azure service user from token (MobileServiceUser)
11.) Associate user to azure service object
MobileServiceClient.CurrentUser =
MobileServiceClient.CurrentUser.MobileServiceAuthenticationToken =
12.) Perform synchronisation
MobileServiceClient.SyncContext.PushAsync()
MobileServiceClient.GetSyncTable().PullAsync()

My problem is, when i do flow A->B->C, my synchronisation is not returning from PushAsync().
Once I force closed my app and re-launch into flow A->C, everything will work properly (PushAsync() completes and PullSync() retrieve data from remote).
I also tried to remove the "redundant" PushAsync() in step #12, but still face the same issue where synchronisation became not returning from PullAsync().

Answers

  • AlexDunnAlexDunn USMember ✭✭✭

    Have you tried switching the order of your sync?
    i.e. Calling PullAsync then PushAsync

  • PierceBogganPierceBoggan USForum Administrator, Xamarin Team, Developer Group Leader Xamurai
    edited January 2017

    Here is my code for syncing with Azure Mobile Apps. Pretty minimal, but handles online/offline sync and basic conflicts.

    The Contoso Moments sample is also very good for more advanced scenarios.

  • LimYeenFeiLimYeenFei MYMember ✭✭

    thanks for the reply, but i couldn't find the reason why my implementation doesn't work yet. the given references has more or less the same process outlined in xamarin and microsoft website.

    I'm suspecting this has something to do with threading (though I'm using async/await extensively and these should has nothing to do with threading) because once in a while the same procedure (first launch + first sync) will able to work (returned properly).

  • LimYeenFeiLimYeenFei MYMember ✭✭

    I've tried to wrap step #12 within await Task.Run() so that it can be executed from non-main thread, still having the same issue

Sign In or Register to comment.