JadedEric
JadedEric

Reputation: 2103

Azure App Services throws database not found when doing a retrieval

I have Azure App services in my Xamarin application, and everything was working 100% until this morning.

After a while, I found, what looks like the SDK removing my database from local storage, as soon as I do the .GetSyncTable() call.

My service looks like this:

private readonly IMobileServiceSyncTable<User> _table;
private readonly IMobileServiceClient _client;
private readonly MobileServiceSQLiteStore _store;
private readonly ICryptography _cryptography;
private readonly ISettings _settings;

public UserService(
    IMobileServiceClient client,
    ICryptography cryptography)
{
    _client = client;
    _cryptography = cryptography;
    _settings = new Settings(cryptography) as ISettings;

    _store = new MobileServiceSQLiteStore(Settings.SyncDb);
    _store.DefineTable<User>();

    _client.SyncContext.InitializeAsync(_store);

    _table = _client.GetSyncTable<User>();
}

and then I do an "All()", which looks like this:

public async Task<List<User>> All()
{
    try
    {
        var users = await _table.ToListAsync(); // <- this throws "table" not defined
        return users;
    }
    catch (SQLiteException sqlex)
    {
        Log.Warning("UserService", sqlex.Message);
    }
    catch (Exception ex)
    {
        Log.Warning("UserService", ex.Message);
    }
}

I have been trying to figure this out, and I'm nowhere closer to a solution.

During my debug, if I land my debugger on the await, and interrogate the _store variable, my "user" table is defined, then a few seconds later, _store no longer contains my table.

I used ADB to download the local store, and viewed it in a SQLite manager, and the table is indeed defined, just, for some reason, it gets "lost"?

Any ideas would be greatly appreciated.

Upvotes: 1

Views: 55

Answers (1)

Bruce Chen
Bruce Chen

Reputation: 18465

Per my understanding, the issue may caused by the InitializeAsync has not been initialized correctly. You just called _client.SyncContext.InitializeAsync(_store); in the constructor of UserService class. And you need to invoke await _client.SyncContext.InitializeAsync(_store); for initializing the store.

The common Offline Sync Initialization would look as follows:

async Task InitializeAsync()
{
     // Short circuit - local database is already initialized
     if (Client.SyncContext.IsInitialized)
         return;

     // Create a reference to the local sqlite store
     var store = new MobileServiceSQLiteStore("offlinecache.db");

     // Define the database schema
     store.DefineTable<TodoItem>();

     // Actually create the store and update the schema
     await Client.SyncContext.InitializeAsync(store);
}

Here is a similar issue about wrapping the operations against Azure Cloud Table. Moreover, you could follow adrian hall's book about An Offline Client.

Upvotes: 1

Related Questions