2016-12-15 54 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都在同一时间发生。这显然很糟糕。所以,但是把它们放在同一个方法中,等待它们分开运行,它们按预期工作。

相关问题