2016-07-05 10 views
1

私はAzure Mobile Servicesを使用して、Azureデータベース(Xamarin)でAndroidアプリを構築しています。 私はそのレコードからテーブルをクリアしたいです。
'table.RemoveAsync'がありますが、すべての行を選択する方法がわかりません。
「選択」または「どこ」ですか? 助けていただければ幸いです。Android Azureすべての行を削除します

//Mobile Service Client reference 
    private MobileServiceClient client; 

    //Mobile Service sync table used to access data 
    private IMobileServiceSyncTable<Mashlim> mashlimTable; 

    const string applicationURL = @"http://blahblah.azurewebsites.net/"; 


    public override async void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     CurrentPlatform.Init(); 

     // Create the Mobile Service Client instance, using the provided 
     // Mobile Service URL 
     client = new MobileServiceClient(applicationURL); 
     await InitLocalStoreAsync(); 

     // Get the Mobile Service sync table instance to use 
     mashlimTable = client.GetSyncTable<Mashlim>(); 

    } 

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     var view = inflater.Inflate(Resource.Layout.ShabbatMinyan, container, false); 
     …. 

     OnRefreshItemsSelected(); 

     return view; 
    } 

    private async Task InitLocalStoreAsync() 
    { 
     // new code to initialize the SQLite store 
     string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), localDbFilename); 

     if (!File.Exists(path)) 
     { 
      File.Create(path).Dispose(); 
     } 

     var store = new MobileServiceSQLiteStore(path); 
     store.DefineTable<Mashlim>(); 

     // Uses the default conflict handler, which fails on conflict 
     // To use a different conflict handler, pass a parameter to InitializeAsync. For more details, see http://go.microsoft.com/fwlink/?LinkId=521416 
     await client.SyncContext.InitializeAsync(store); 
    } 

    private async Task SyncAsync() 
    { 
     try 
     { 
      await client.SyncContext.PushAsync(); 
      await mashlimTable.PullAsync("allMashlims", mashlimTable.CreateQuery()); // query ID is used for incremental sync 
     } 
     catch (Java.Net.MalformedURLException) 
     { 
      CreateAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error"); 
     } 
     catch (Exception e) 
     { 
      CreateAndShowDialog(e, "Error"); 
     } 
    } 

    // Called when the refresh menu option is selected 
    private async void OnRefreshItemsSelected() 
    { 
     await SyncAsync(); // get changes from the mobile service 
     await RefreshItemsFromTableAsync(); // refresh view using local database 
    } 

    //Refresh the list with the items in the local database 
    private async Task RefreshItemsFromTableAsync() 
    { 
     try 
     { 
      // Get the items that were marked as mashlim and add them to list 
      var list = await mashlimTable.Where(item => item.IsMashlim == true).ToListAsync(); 

      mashlimim = 0; 
      foreach (Mashlim current in list) 
       mashlimim++; 

      mashlimimNumText.Text = mashlimim.ToString(); 
     } 
     catch (Exception e) 
     { 
      CreateAndShowDialog(e, "Error"); 
     } 
    } 

    [Java.Interop.Export()] 
    public async void AddItem() 
    { 
     if (client == null) 
     { 
      return; 
     } 

     if(Settings.MashlimId==string.Empty) 
     { 

      // Create a new item 
      item = new Mashlim 
      { 
       Name = nameText.Text, 
       PhoneNumber = phoneText.Text, 
       IsMashlim = true 
      }; 


      try 
      { 
       await mashlimTable.InsertAsync(item); // insert the new item into the local database 
       await SyncAsync(); // send changes to the mobile service 
       await RefreshItemsFromTableAsync(); 
       Settings.MashlimId = item.Id; 
       Settings.MashlimName = item.Name; 
       Settings.IsMashlim = true; 
       Settings.MashlimPhone = item.PhoneNumber; 
      } 
      catch (Exception e) 
      { 
       CreateAndShowDialog(e, "Error"); 
      } 
     } 

     else 
     { 
      Settings.IsMashlim = true; 
      item = new Mashlim 
      { 
       Id = Settings.MashlimId, 
       Name = Settings.MashlimName, 
       IsMashlim = Settings.IsMashlim, 
       PhoneNumber = Settings.MashlimPhone 
      }; 


      try 
      { 
       await mashlimTable.UpdateAsync(item); // insert the new item into the local database 
       await SyncAsync(); // send changes to the mobile service 
       await RefreshItemsFromTableAsync(); 
       mashlim = true; 
      } 
      catch (Exception e) 
      { 
       CreateAndShowDialog(e, "Error"); 
      } 
     } 
    } 

} 
} 

ありがとう:

は、ここに私のコードです。

答えて

2

クライアントSDKでは、ローカルのSQLiteストアで任意のSQL文を実行することはできません。したがって、すべての行を選択し、それらをループして削除する必要があります。 ToListAsyncを使用してすべてをメモリにロードすることに注意してください(デバイス上のメモリが不足する可能性があります)。X行がある場合、Xサーバ要求が発生しますが、これはかなり遅くなる可能性があります。クライアントでリクエストを行う方法については、How to: Delete data in a mobile appを参照してください。

特定の条件に一致するすべての行を削除する場合は、カスタムAPIをサーバーに作成し、クライアントがAPIを呼び出すだけで1つの要求を送信する方がよい場合があります。サーバーでは、.NETまたはNode.jsのどちらを使用しているかに応じて、SQLまたはLINQのいずれかを使用できます。

たとえば、.NETバックエンドを使用して、How to: Define a custom API controllerを使用してカスタムAPIを作成します。

using (var context = new YourContext()) 
{ 
    context.ExecuteStoreCommand("DELETE FROM YOURTABLE WHERE Condition = value"); 
} 
+0

ありがとう:

は、お使いのコントローラのいずれかの方法では、次のコードを持っているでしょう。私は本当にこれが初めてで、何をすべきかわからないので、コードを使ってこれを行う方法について、より具体的なガイダンスを提供してください。私はNode.jsではなくC#を使用しています。ありがとう! – amitairos

+0

私の行の数が約100を超えないことが分かっている場合は、それらをすべて選択しないといけませんか?これはどうすればいいですか?ありがとう? – amitairos

+1

@amitairos私はあなたがサーバ上で書いてくれるSQLの例を使って答えを更新しました。 100行未満の場合は、ToListAsyncを使用してクライアント上でループすることもできますが、削除操作ごとに1つのサーバー要求があります。クライアントで操作を行いたい場合は、パフォーマンステストを行うことをお勧めします。 –

関連する問題