AzureStore.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.WindowsAzure.MobileServices;
  7. using Microsoft.WindowsAzure.MobileServices.Sync;
  8. using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
  9. using DevDaysSpeakers.Model;
  10. using System.Diagnostics;
  11. namespace DevDaysSpeakers.ViewModel
  12. {
  13. public class AzureStore
  14. {
  15. static AzureStore current;
  16. public static AzureStore Current
  17. {
  18. get
  19. {
  20. if (current == null)
  21. current = new AzureStore();
  22. return current;
  23. }
  24. }
  25. MobileServiceClient MobileService { get; set; }
  26. IMobileServiceSyncTable<Speaker> speakerTable;
  27. public async Task Initialize()
  28. {
  29. if (MobileService != null)
  30. return;
  31. MobileService = new MobileServiceClient("https://montemagnospeakers.azurewebsites.net");
  32. var store = new MobileServiceSQLiteStore("speakers.db");
  33. store.DefineTable<Speaker>();
  34. await MobileService.SyncContext.InitializeAsync(store);
  35. speakerTable = MobileService.GetSyncTable<Speaker>();
  36. }
  37. public async Task<List<Speaker>> GetSpeakers()
  38. {
  39. await Initialize();
  40. try
  41. {
  42. await speakerTable.PullAsync("allSpeakers", speakerTable.CreateQuery());
  43. }
  44. catch (Exception ex)
  45. {
  46. Debug.WriteLine(ex);
  47. }
  48. return await speakerTable.ToListAsync();
  49. }
  50. }
  51. }