2017-11-22 8 views
0

どちらが良いですか?各要素のための取引に1つのトランザクション内の領域ループVS 1つのステップループごとに1つのトランザクション

public void saveProducts(List<Product> products) { 

    Realm realm = Realm.getDefaultInstance(); 
    realm.executeTransaction(realm1 -> { 

     for (Product product : products) { 
      Number currentIdNum = realm1.where(Product.class).max("id"); 
      long nextId; 
      if (currentIdNum == null) { 
       nextId = 1; 
      } else { 
       nextId = currentIdNum.longValue() + 1; 
      } 

      String code = product.getCategory().getCode(); 
      Category code1 = realm.where(Category.class).equalTo("code", code).findFirst(); 
      if (code1 == null) { 
       Category category = realm.copyToRealmOrUpdate(product.getCategory()); 
       product.setCategory(category); 
      } else { 
       product.setCategory(code1); 
      } 
      product.setId(nextId); 
      realm1.insertOrUpdate(product); // using insert API 
     } 
    }); 
    realm.close(); 
} 
  • ループ内ループを有する

    1. つのトランザクション?

      public void saveProducts(List<Product> products) { 
      
          Realm realm = Realm.getDefaultInstance(); 
          for (Product product : products) { 
           realm.executeTransaction(realm1 -> { 
            Number currentIdNum = realm1.where(Product.class).max("id"); 
            long nextId; 
            if (currentIdNum == null) { 
             nextId = 1; 
            } else { 
             nextId = currentIdNum.longValue() + 1; 
            } 
      
            String code = product.getCategory().getCode(); 
            Category code1 = realm.where(Category.class).equalTo("code", code).findFirst(); 
            if (code1 == null) { 
             Category category = realm.copyToRealmOrUpdate(product.getCategory()); 
             product.setCategory(category); 
            } else { 
             product.setCategory(code1); 
            } 
            product.setId(nextId); 
            realm1.insertOrUpdate(product); // using insert API 
      
           }); 
          } 
          realm.close(); 
      } 
      

    それとも別の方法はありますか?あなたがそれをより良くする方法を知っているならば、どうか言ってください。

  • 答えて

    0

    レルムプロキシオブジェクトを扱っているので、このアプローチに従うことができます。

    public void saveProducts(List<Product> products) { 
    
        Realm realm = Realm.getDefaultInstance(); 
         realm.beginTransaction(); 
    
    
        for (Product product : products) { 
    
    
         Number currentIdNum = realm1.where(Product.class).max("id"); 
         long nextId; 
         if (currentIdNum == null) { 
          nextId = 1; 
         } else { 
          nextId = currentIdNum.longValue() + 1; 
         } 
    
         String code = product.getCategory().getCode(); 
         Category code1 = realm.where(Category.class).equalTo("code", code).findFirst(); 
         if (code1 == null) { 
          Category category = realm.copyToRealmOrUpdate(product.getCategory()); 
          product.setCategory(category); 
         } else { 
          product.setCategory(code1); 
         } 
         product.setId(nextId); 
         realm.insertOrUpdate(product); // using insert API 
    
    
    
        } 
        realm.commitTransaction(); 
    
        realm.close(); 
    } 
    
    +0

    アイテムごとに新しいトランザクションを開始するのはなぜですか? – EpicPandaForce

    +0

    @EpicPandaForce彼は単一のオブジェクト変更リスナーが必要であると仮定した場合のシナリオでこれを行いました。私は、より良いアプローチと見なすことができる単一のinitとコミットのための私の答えを更新しました。 – Aks4125

    +1

    それは私のために働いた。より良く受け入れられた答え。ありがとう;)+1 –

    1
    for (Product product : products) { 
        realm.executeTransaction(realm1 -> 
    

    この1つははるかに悪いです。

    単一トランザクションを優先します。

    +0

    ありがとうございます。そして、1対多を保存するアプローチは正しいですか?私のコード – ip696

    +0

    カテゴリにプライマリキーがあるかどうかによって異なりますが、それは問題ありません。私は 'code'がPKであると推測しています – EpicPandaForce

    +0

    public class CategoryはRealmObjectを拡張します{ @PrimaryKey プライベートString code; プライベート文字列名。 @LinkingObjects( "category") プライベートfinal RealmResults products = null; – ip696

    関連する問題