我正在从设置PrimaryKey为状态的json api中获取数据。这是我正在获取的Json数据:

records[
  {
         "id":"192693681",
         "timestamp":"1500204608",
         "state":"Rajasthan",
         "district":"Rajasamand",
         "market":"Rajasamand",
         "commodity":"Sugar",
         "variety":"Shakkar",
         "arrival_date":"16/07/2017",
         "min_price":"4000",
         "max_price":"4100",
         "modal_price":"4050"
      },
      {
         "id":"192693701",
         "timestamp":"1500204608",
         "state":"Rajasthan",
         "district":"Rajasamand",
         "market":"Rajasamand",
         "commodity":"Wheat",
         "variety":"Farmi",
         "arrival_date":"16/07/2017",
         "min_price":"1600",
         "max_price":"1650",
         "modal_price":"1625"
      },
      {
         "id":"192693721",
         "timestamp":"1500204608",
         "state":"Rajasthan",
         "district":"Rajasamand",
         "market":"Rajasamand",
         "commodity":"Wheat",
         "variety":"Lokwan",
         "arrival_date":"16/07/2017",
         "min_price":"1550",
         "max_price":"1600",
         "modal_price":"1575"
      }
   ]


这是我的查询,用于返回每个州的市场数据:

private void populateMarketData(String state){
        cityAdapter = new CityAdapter(mRealm.where(Records.class).equalTo(Constants.STATE, state).findAll(), this);
        cityRecyclerView.setAdapter(cityAdapter);
        Log.d(TAG, "Total Cities" + String.valueOf(cityAdapter.getData().size()));
        cityAdapter.notifyDataSetChanged();
    }


这是我的查询,用于将州的所有商品退还给市场:

 @Override
    public void onMarketClicked(String cityName) {
        tradeAdapter = new TradeAdapter(mRealm.where(Records.class)
                .equalTo(Constants.MARKET, cityName).findAll(), this);
        tradeRecyclerView.setAdapter(tradeAdapter);
    }


这是GcmTaskService,用于更新后台服务中的数据:

Realm.init(mContext);
        List<Records> records = new MandiDataHandler().getMandiQuotes(url);
        SaveMandi saveMandi = new SaveMandi(state, records);
        Realm realm = new RealmController(getApplication()).getRealm();
        realm.beginTransaction();
        realm.copyToRealmOrUpdate(saveMandi);
        realm.commitTransaction();
        realm.close();


这是DataHelper,用于保存Json API中的数据

@PrimaryKey
    private String state;

    private RealmList<Records> recordsRealmList = new RealmList<>();

    public SaveMandi(){}

    public SaveMandi(String state, List<Records> recordsList) {

        try {
            this.state = state;
            for (Records records: recordsList ) {
                records = new Records(records.getId(), DateUtils.getRelativeTimeSpanString(
                        Long.parseLong(records.getTimestamp())).toString(), records.getState(), records.getMarket(),
                        records.getCommodity(), records.getVariety(), records.getMin_price(), records.getMax_price());
                this.recordsRealmList.add(records);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }


我的RealmRecyclerView中的问题是,当我将PrimaryKey设置为state时,它会返回单个项目;否则,当我将PrimaryKey设置为id时,它将返回多个重复数据。我不确定我可能在哪里错。

最佳答案

您想选择PrimaryKey为仅当它们等于(等于)“等于”时才匹配的东西,就像equals()方法一样。如果将两个具有相同RealmObjectsPrimaryKey放入Realm中,则其中只有一个会出现在Realm中,因为每个不同的PrimaryKey只能有一个对象。

例如,如果我在域中有一个RealmObject aPrimaryKey1,并且我又将另一个RealmObject bPrimaryKey1放入了Realm,则a将被b删除,因为每个PrimaryKey只能有一个对象。

查阅Realm数据库的一个好工具是Stetho,我强烈建议使用它来调试使用Realm的应用程序。

09-25 16:54