境界返回错误的结果

时间:2016-11-04 19:44:30

标签: android realm

我从服务器收到一个对象列表。每个Object与其他对象有很多对多关系,我将服务器响应保存到领域,并且所有内容在领域浏览器上看起来都很好。但是当我在这个对象上调用一个All时,有些孩子会带有错误的值。 例如:

JSON响应:

    [{
        "id": 1127219,
        "products": [{
            "name": "product 1",
            "id": "519_1246"
        }, {
            "name": "product 2",
            "id": "519_1791"
        }],
        "steps": [{
            "action": "go_store",
            "location": {
                "id": 519,
                "lat": "4.675863000000",
                "lng": "-74.053015000000"
            },
            "id": "go_store-1127219"
        }, {
            "action": "take_product",
            "location": null,
            "id": "take_product-1127219"
        }, {
            "action": "go_home",
            "location": {
                "id": 367368,
                "lat": "4.679032500000",
                "lng": "-74.055361700000"
            },
            "id": "go_home-1127219"
        }]
    }, {
        "id": 1127333,
        "products": [{
            "name": "Bebidas Starbucks",
            "id": "10000349_90220"
        }],
        "steps": [{
            "action": "take_product",
            "location": null,
            "id": "take_product-1127333"
        }, {
            "action": "go_home",
            "location": {
                "id": 367368,
                "lat": "4.679032500000",
                "lng": "-74.055361700000"
            },
            "id": "go_home-1127333"
        }]
    }]

RealmObjects:

       Order extends RealmObject {
             @PrimaryKey int id;
             RealmList<Product> products;
             RealmList<Step> steps;
        }

        Product extends RealmObject {
            @PrimaryKey int id;
            String name;
        }
        Step extends RealmObject {
             @PrimaryKey int id;
             RealmList<Position> position;
        }
        Position extends RealmObject {
             @PrimaryKey int id;
             double lat, lng;
        }

的Rx

public Observable<List> getAll(Class clazz) {
    return Observable.defer(() -> {
        mRealm = Realm.getDefaultInstance();
        Observable o = Observable.just(mRealm.copyFromRealm(mRealm.where(clazz).findAll()));
        mRealm.closeRealm();
        return o;
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}

public Observable<?> putAll(@NonNull JSONArray jsonArray, String idColumnName, @NonNull Class dataClass) {
    return Observable.defer(() -> {
        try {
            updateJsonArrayWithIdValue(jsonArray, idColumnName, dataClass);
        } catch(@NonNull JSONException | IllegalArgumentException e) {
            return Observable.error(e);
        }
        mRealm = Realm.getDefaultInstance();
        executeWriteOperationInRealm(mRealm, () -> mRealm.createOrUpdateAllFromJson(dataClass, jsonArray));
        mRealm.close();
        return Observable.just(Boolean.TRUE);
    }
}

@NonNull
private JSONArray updateJsonArrayWithIdValue(@NonNull JSONArray jsonArray, @Nullable String idColumnName, Class dataClass)
        throws JSONException, IllegalArgumentException {
    if(idColumnName == null || idColumnName.isEmpty()) {
        throw new IllegalArgumentException(mContext.getString(R.string.no_id));
    }
    for(int i = 0, length = jsonArray.length(); i < length; i++) {
        if(jsonArray.get(i) instanceof JSONObject) {
            updateJsonObjectWithIdValue(jsonArray.getJSONObject(i), idColumnName, dataClass);
        }
    }
    return jsonArray;
}

@NonNull
private JSONObject updateJsonObjectWithIdValue(@NonNull JSONObject jsonObject, @Nullable String idColumnName, Class dataClass)
        throws JSONException, IllegalArgumentException {
    if(idColumnName == null || idColumnName.isEmpty()) {
        throw new IllegalArgumentException(mContext.getString(R.string.no_id));
    }
    if(jsonObject.getInt(idColumnName) == 0) {
        jsonObject.put(idColumnName, Utils.getNextId(dataClass, idColumnName));
    }
    return jsonObject;
}

private void executeWriteOperationInRealm(@NonNull Realm realm, @NonNull Executor executor) {
    if(realm.isInTransaction()) {
        realm.cancelTransaction();
    }
    realm.beginTransaction();
    executor.run();
    realm.commitTransaction();
}

所以我收到了2个订单的清单,每个订单都有自己的产品和步骤。使用调试器和领域浏览器似乎一切都保存得很好,但是当我getAll();时,响应是具有正确产品的订单,但两者都具有相同的步骤,而不是各自的步骤。所有id值都来自服务器。请帮助我,我已经被困在这一段时间了。提前谢谢

0 个答案:

没有答案