Xamarin Forms Android project not connecting to my Azure hosted Database

JohnButlerJohnButler USMember ✭✭
edited November 2017 in Cross Platform with Xamarin

Has anyone followed this Tutorial lately?
https://youtu.be/PSgTGTMUk3o

I followed it and then downloaded his github example of just the front end
https://github.com/jamesmontemagno/app-coffeecups

I got it almost fully working. I can post to the server with Postman. I can also manually add data to the table in VS through backend project.
But I can not get the front end, Android app working. It just stores stuff locally, it never gets to the server, push or pull. But something that is weird is when a terminate the app and run it again it does not load any of previous data. So it may not even be storing it locally either, that or its wiping it everytime.

This is the code for the AzureService. For any of the other code it is the same as the project sited above.

namespace PillTrackerApp.Services
{

class AzureService
{
    public MobileServiceClient Client { get; set; } = null;
    private IMobileServiceSyncTable<Pill> pillTable;
    public static bool UseAuth { get; set; } = false;

    public async Task Initialize()
    {

        if (Client?.SyncContext?.IsInitialized ?? false)
            return;

        var appUrl = "http://MY-Address-Goes-Here.azurewebsites.net";

        Client = new MobileServiceClient(appUrl);

        var path = "syncstore.db";
        //path = Path.Combine(MobileServiceClient.DefaultDatabasePath, path);

        var store = new MobileServiceSQLiteStore(path);
        store.DefineTable<Pill>();
        await Client.SyncContext.InitializeAsync(store);
        pillTable = Client.GetSyncTable<Pill>();
    }

    public async Task SyncPill()
    {            
        try
        {
            if (!CrossConnectivity.Current.IsConnected)
                return;

    await pillTable.PullAsync("allPills", pillTable.CreateQuery());
            await Client.SyncContext.PushAsync();
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Unable to sync pills, that is alright as we have offline capabilities: " + ex);
        }
    }

    public async Task<IEnumerable<Pill>> GetPills()
    {
        await Initialize();
        await SyncPill();

        return await pillTable.OrderBy(c => c.DateUtc).ToEnumerableAsync();
    }

    public async Task<Pill> AddPill(bool atHome, string location)
    {
        await Initialize();            
        var pill = new Pill()
        {
            DateUtc = DateTime.UtcNow, 
            TakenAtHome = atHome,
            OS = Device.RuntimePlatform,
            Location = location ?? string.Empty
        };
        await pillTable.InsertAsync(pill);
        await SyncPill();

        return pill;
    }
}

}

I have F9 these two lines
await pillTable.PullAsync("allPills", pillTable.CreateQuery());
await Client.SyncContext.PushAsync();
And it never seems to run them it just exits the try statement. I even ran then before the try statement and nothing, but no exception was thrown either...

I would love any insight! I did type it all by hand and I didn't copy and paste so I might of messed something up, but I cant find it if that was the issue

Answers

  • JohnButlerJohnButler USMember ✭✭

    Is the question I am asking not clear/ confusing/ makes no sense? Sorry if it is.
    In the time since the post I was able to get a different project on Android to talk with an easy table. But I need it to work with the project above that uses a c# backend.
    Thanks!

Sign In or Register to comment.