我有一个realm对象,其中包含带有ID的项的列表
@SerializedName("products")
RealmList<ProductDialItem> products;
@SerializedName("previous_suppliers")
RealmList<SuppliersDialItem> previousSuppliers;
@SerializedName("tariffs")
RealmList<TariffsDialItem> tariffs;
@SerializedName("deposit_frequencies")
RealmList<SimpleDialItem> depositFrequencies;
@SerializedName("bank_codes")
RealmList<SimpleDialItem> bankCodes;
@SerializedName("gas_usages")
RealmList<SimpleDialItem> gasUsages;以此类推,list中的每一项都有ID。服务器发送一个json响应,其中包含要在对象中填充的值。
但是我们也使用Last-Modified报头,这样服务器就只发送修改过的条目。我只想更新项目,而不是删除任何项目。
Tl;dr如何仅更新/添加项目到领域,而不删除它们
发布于 2016-09-05 21:46:46
让对象包含:
class MyClass{
int x = 0;
int y = 0;
int z = 0;
}然后,如果您需要从web递增变量:
MyClass finalObject = new MyClass();
MyClass tempObject = new Gson().fromJson(response,MyClass.class);
incrementObjects(tempObject,finalObject);现在创建方法incrementObjects():
private void incrementObjects(MyClass obj1, MyClass obj2){
obj2.x += obj1.x;
obj2.y += obj1.y;
obj2.z += obj1.z;
}发布于 2016-09-05 21:47:52
当你得到更新的数据响应时,你可以在Realm上查询,
//Create RealmList when you get updated Data in your api response.
RealmList<YourModel> updatedData = yourResponseData.getData();
//Get data from localDatabase and Query on
RealmQuery<YourModel> where = mRealm.where(YourModel.class);
for (int i = 0; i < updatedData.size(); i++) {
//put condition to match data based on your database.
where = where.notEqualTo(YourModel.NAME,updatedData.get(i).getName());
}
//Now this where.findAll(); , will return list of data which is notEqual To updatedData
//perform update operation with this or deleteFrom local Data and insert new/updated Data.
where.findAll();https://stackoverflow.com/questions/39331614
复制相似问题