Skip to content

Instantly share code, notes, and snippets.

@winstongubantes
Created February 9, 2017 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winstongubantes/3323a77c3ac4f88bd0127b77eddac00f to your computer and use it in GitHub Desktop.
Save winstongubantes/3323a77c3ac4f88bd0127b77eddac00f to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConversationApp.Models;
using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Microsoft.WindowsAzure.MobileServices.Sync;
namespace ConversationApp.Services
{
public class AzureDataService
{
private MobileServiceClient _mobileServiceClient = new MobileServiceClient("http://conversationapp.azurewebsites.net");
IMobileServiceSyncTable<Message> _messageTable;
public async Task Initialize()
{
if (_mobileServiceClient?.SyncContext?.IsInitialized ?? false)
return;
const string path = "syncstore.db";
//setup our local sqlite store and intialize our table
var store = new MobileServiceSQLiteStore(path);
store.DefineTable<Message>();
await _mobileServiceClient.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
//Get our sync table that will call out to azure
_messageTable = _mobileServiceClient.GetSyncTable<Message>();
}
public async Task<IEnumerable<Message>> GetMessages()
{
await Initialize();
await SyncMessage();
return await _messageTable.ToEnumerableAsync();
}
public async Task AddMessage(Message msg)
{
await Initialize();
await _messageTable.InsertAsync(msg);
await SyncMessage();
}
public async Task SyncMessage()
{
try
{
await _messageTable.PullAsync("allMessage", _messageTable.CreateQuery());
await _mobileServiceClient.SyncContext.PushAsync();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment