领域:更新外部obect的id

时间:2016-12-13 19:18:02

标签: java android json realm

我从API获取JSON业务列表。此列表可以更改,因此我需要定期调用它来更新数据。 每个商家都有一个地址,但地址没有 id 。因为,它是服务器中的 OneToOne 关系,因此无需拥有主键。但在客户端,Realm需要和主键使用 copyToRealmOrUpdate()。所以我在地址类中添加了 id 以便能够使用它

这是我的代码

商务舱

public class Business extends RealmObject{

@PrimaryKey
private int id;
private Category category;
private String name;
private String description;


private String telephone;
private String fax;
private String email;
private String website;
private Address address;

//getters and setters
}

地址类

public class Address extends RealmObject{


@PrimaryKey
private int id ;
@SerializedName("street_type")
private String streetType;
@SerializedName("street_number")
private String streetNumber;
@SerializedName("street_name")
private String streetName;
private String direction;
private String city;
private String province;
@SerializedName("postal_code")
private String postalCode;

public Address (){    }

public void setAddress(Address a){
    this.streetName = a.getStreetName();
    this.streetType = a.getStreetType();
    this.direction = a.getDirection();
    this.city = a.getCity();
    this.province = a.getProvince();
    this.streetNumber = a.getStreetNumber();
    this.postalCode = a.getPostalCode();
}
//other getters and setters
}

JSON示例

[
{
    "id": 2,
    "address": {
        "business": 2,
        "street_number": 1111,
        "street_type": "ST",
        "street_name": " STREET NAME",
        "direction": "",
        "city": "CITY",
        "province": "QC",
        "postal_code": "H0H 0H0"
    },
    "category": {
        "id": 1,
        "name": "Restaurant",
        "active": true
    },
    "name": "Business",
    "description": "a description",
    "email": "",
    "person_in_charge": "",
    "website": "",
    "telephone": "(514) 555-5555",
    "fax": "",
},
//others business
]

从api检索商家

 Realm realm = Realm.getDefaultInstance();
        List<Business> businesses = null;
        try {
            businesses = new CallGenerator<List<Business>, BusinessService>().asynAPICall(
                    new CallGenerator.AsynAPICallback<BusinessService>() {
                        @Override
                        public Call onApiCall(BusinessService client) {
                            return client.getBusinessesByCategory(null);
                        }
                    }, BusinessService.class).execute().get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        for (Business b : businesses) {
            Address realmAddress = realm.where(Address.class).equalTo("id", b.getId()).findFirst();

            realm.beginTransaction();

            if (realmAddress == null)
                realmAddress = realm.createObject(Address.class, b.getId());
            realmAddress.setAddress(b.getAddress());
            b.setAddress(realmAddress);
            realm.copyToRealmOrUpdate(b);
            realm.commitTransaction();
    }

问题是每个业务中的地址永远不会保存,因此在我调用它们时始终为null。我在这样查询时可以得到地址:

Address a = realm.where(Address.class).equalTo("id",business.id).findFirst();

但不是这样的:

Business mBusiness = realm.where(Business.class).equalTo("id", id).findFirst();
mBusiness.getAddress//always null

1 个答案:

答案 0 :(得分:0)

       Address realmAddress = realm.createObject(Address.class,b.getId());

应该是

       Address realmAddress = realm.where(Address.class).equalTo("id", b.getId()).findFirst();
       if(realmAddress == null) {
           realm.createObject(Address.class,b.getId());
       }

但如果你想优化它,可能是

realm.executeTransaction(new Realm.Transaction() {
  @Override
  public void execute(Realm realm) {
    Address realmAddress = new Address();
    for (Business b : businesses) {
        realmAddress.setId(b.getId())
        realmAddress.setStreetName(b.getAddress().getStreetName());
        realmAddress.setStreetType(b.getAddress().getStreetType());
        realmAddress.setDirection(b.getAddress().getDirection());
        realmAddress.setCity(b.getAddress().getCity());
        realmAddress.setProvince(b.getAddress().getProvince());
        realmAddress.setStreetNumber(b.getAddress().getStreetNumber());

        realm.insertOrUpdate(realmAddress);
        Log.i(TAG,"New address id "+realmAddress.getId() +" with address "+ realmAddress);
    }
    List<Business> realmRepos = realm.copyToRealmOrUpdate(businesses);
  }
});