2016-12-15 9 views
0

xamarinとazureのモバイルサービスを使用してアプリを作っています。私はオフラインの同期機能を追加しようとしていますが、私は立ち往生しています。 System.NullReferenceException: Object reference not set to an instance of an object.SyncContraceptionCenters()が実行されています。私は、私はこのエラーを取得しています。このSyncTableを同期しようとするとXamarinアプリがクラッシュする

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"]); 
       } 
      } 
     } 

のように見えるサービスを提供しています。私が言う限り、私は私のサービスでcoffeeItemsの例を再現すると言うことができますが、私は立ち往生しています。

答えて

0

解決策が見つかりました。問題はテーブルの同期方法でした。

SyncShop()を同時に呼び出すと、shopItemtable.PullAsynccontraceptionTable.PullAsyncが同時に発生しています。明らかに悪い悪いです。だから、同じ方法でそれらを入れ、それらを待っている彼らは別々に実行し、彼らは期待どおりに動作します。

関連する問題