2016-12-15 55 views
0

我使用xamarin和azure移動服務製作應用程序。我試圖添加脫機同步功能,但我被卡住了。我有一個看起來像這樣Xamarin應用程序在嘗試同步SyncTable時崩潰

class AzureService 
    { 

     public MobileServiceClient Client; 

     AuthHandler authHandler; 
     IMobileServiceTable<Subscription> subscriptionTable; 
     IMobileServiceSyncTable<ShopItem> shopItemTable; 
     IMobileServiceSyncTable<ContraceptionCenter> contraceptionCenterTable; 
     IMobileServiceTable<Member> memberTable; 
     const string offlineDbPath = @"localstore.db"; 


     static AzureService defaultInstance = new AzureService(); 
     private AzureService() 
     { 
      this.authHandler = new AuthHandler(); 
      this.Client = new MobileServiceClient(Constants.ApplicationURL, authHandler); 

      if (!string.IsNullOrWhiteSpace(Settings.AuthToken) && !string.IsNullOrWhiteSpace(Settings.UserId)) 
      { 
       Client.CurrentUser = new MobileServiceUser(Settings.UserId); 
       Client.CurrentUser.MobileServiceAuthenticationToken = Settings.AuthToken; 
      } 

      authHandler.Client = Client; 

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

      //setup our local sqlite store and intialize our table 
      var store = new MobileServiceSQLiteStore(offlineDbPath); 

      //Define sync table 
      store.DefineTable<ShopItem>(); 
      store.DefineTable<ContraceptionCenter>(); 

      //Initialize file sync context 
      //Client.InitializeFileSyncContext(new ShopItemFileSyncHandler(this), store); 

      //Initialize SyncContext 
      this.Client.SyncContext.InitializeAsync(store); 

      //Tables 
      contraceptionCenterTable = Client.GetSyncTable<ContraceptionCenter>(); 
      subscriptionTable = Client.GetTable<Subscription>(); 
      shopItemTable = Client.GetSyncTable<ShopItem>(); 
      memberTable = Client.GetTable<Member>(); 

     } 

     public static AzureService defaultManager 
     { 
      get { return defaultInstance; } 
      set { defaultInstance = value; } 
     } 

     public MobileServiceClient CurrentClient 
     { 
      get { return Client; } 
     } 
public async Task<IEnumerable<ContraceptionCenter>> GetContraceptionCenters() 
     { 
      try 
      { 
       await this.SyncContraceptionCenters(); 
       return await contraceptionCenterTable.ToEnumerableAsync(); 
      } 
      catch (MobileServiceInvalidOperationException msioe) 
      { 
       Debug.WriteLine(@"Invalid sync operation: {0}", msioe.Message); 
      } 
      catch (Exception e) 
      { 
       Debug.WriteLine(@"Sync error: {0}", e.Message); 
      } 
      return null; 



     } 
public async Task SyncContraceptionCenters() 
     { 

      ReadOnlyCollection<MobileServiceTableOperationError> syncErrors = null; 

      try 
      { 
       //await this.Client.SyncContext.PushAsync(); 

       await this.contraceptionCenterTable.PullAsync(
        //The first parameter is a query name that is used internally by the client SDK to implement incremental sync. 
        //Use a different query name for each unique query in your program 
        "allContraceptionCenters", 
        this.contraceptionCenterTable.CreateQuery()); 
      } 
      catch (MobileServicePushFailedException exc) 
      { 
       if (exc.PushResult != null) 
       { 
        syncErrors = exc.PushResult.Errors; 
       } 
      } 

      // Simple error/conflict handling. A real application would handle the various errors like network conditions, 
      // server conflicts and others via the IMobileServiceSyncHandler. 
      if (syncErrors != null) 
      { 
       foreach (var error in syncErrors) 
       { 
        if (error.OperationKind == MobileServiceTableOperationKind.Update && error.Result != null) 
        { 
         //Update failed, reverting to server's copy. 
         await error.CancelAndUpdateItemAsync(error.Result); 
        } 
        else 
        { 
         // Discard local change. 
         await error.CancelAndDiscardItemAsync(); 
        } 

        Debug.WriteLine(@"Error executing sync operation. Item: {0} ({1}). Operation discarded.", error.TableName, error.Item["id"]); 
       } 
      } 
     } 

我收到此錯誤服務: System.NullReferenceException: Object reference not set to an instance of an object.SyncContraceptionCenters()運行。據我所知,我在我的服務中轉載了coffeeItems示例但我被卡住了。

回答

0

我想我找到了解決方案。問題在於表格的同步方式。

通過調用SyncContraceptionCenters()SyncShop()同時shopItemtable.PullAsynccontraceptionTable.PullAsync都在同一時間發生。這顯然很糟糕。所以,但是把它們放在同一個方法中,等待它們分開運行,它們按預期工作。

相關問題