领域查询不更新android

时间:2017-08-28 19:44:02

标签: android multithreading realm realm-mobile-platform

我在更新查询时遇到错误。这是我得到的错误。

java.lang.IllegalStateException:从错误的线程访问Realm。 Realm对象只能在创建它们的线程上访问。

这是我在IntentService中使用的代码:

public void addPatient(Intent intent){
    long patientID = intent.getLongExtra("id",0);
    Log.v(Constants.TAG, "patientId in datasync " + patientID);

    Realm real;
    real = Realm.getDefaultInstance();

    if(patientID > 0){
        Patient patient = realm.where(Patient.class).equalTo("id",patientID).findFirst();
        if(patient != null){
            JsonObject patientObject = new JsonObject();
            patientObject.add("patient",patient.toJsonObject());
            Log.v(Constants.TAG, "patientData " + patient.toJsonObject());
            try {
                // Simulate network access.
                Log.e(Constants.TAG, "addPatient: appointment ! =null "+patient);
                mNetworkSubscription = NetworkRequest.performAsyncRequest(api.addPatient(patientObject), (data) -> {
                    // Update UI on main thread
                    try {
                        Log.v(Constants.TAG, "result sdsffgfg" + data.getAsJsonObject().get("result"));

                        if (data.getAsJsonObject().get("error") != null) {
                            publishResults("addPatient", STATUS_ERROR, null);
                        }

                        if (data.getAsJsonObject().get("result") != null) {
                            int serverID = data.get("result").getAsInt();
                            Log.v(Constants.TAG, "patientServerId " + serverID);
                            realm.executeTransaction(new Realm.Transaction() {

                                @Override
                                public void execute(Realm realm) {
                                    patient.setServerID(serverID);
                                    realm.copyToRealmOrUpdate(patient);
                                    Log.v(Constants.TAG, "fdfdfdg " + patient);
                                }
                            });


                        }
                    } catch (Exception e) {
                        Log.v(Constants.TAG, "addPatient() exception: " + e.toString());
                        publishResults("addPatient", STATUS_ERROR, null);
                    } finally {
                        publishResults("addPatient", STATUS_FINISHED, null);
                    }
                }, (error) -> {
                    // Handle Error
                    Log.e(Constants.TAG, "addPatient Error: " + error.toString());
                    publishResults("addPatient", STATUS_ERROR, null);
                });

            } catch (Exception e) {
                Log.e(Constants.TAG, "addPatient() Exception: " + e.toString());
                publishResults("addPatient", STATUS_ERROR, null);
            }
        }
    }
}

请检查我哪里出错了。我无法在查询中保存详细信息

1 个答案:

答案 0 :(得分:1)

public void addPatient(final long patientId){
    try(Realm realm = Realm.getDefaultInstance()) {
        if(patientId > 0) {
            Patient patient = realm.where(Patient.class).equalTo("id", patientId).findFirst();
            if(patient != null){
                JsonObject patientObject = new JsonObject();
                patientObject.add("patient", patient.toJsonObject());

                try {
                    // Simulate network access.
                    Log.e(Constants.TAG, "addPatient: appointment ! =null "+patient);
                    mNetworkSubscription = NetworkRequest.performAsyncRequest(api.addPatient(patientObject), (data) -> {
                        // Update UI on main thread
                        try {
                            if (data.getAsJsonObject().get("error") != null) {
                                publishResults("addPatient", STATUS_ERROR, null);
                            }

                            if (data.getAsJsonObject().get("result") != null) {
                                int serverId = data.get("result").getAsInt();
                                Log.v(Constants.TAG, "patientServerId " + serverId);
                                try(Realm r = Realm.getDefaultInstance()) {
                                    r.executeTransaction((_realm) -> {
                                        Patient _patient = _realm.where(Patient.class).equalTo("id", patientId).findFirst();
                                        _patient.setServerId(serverId);
                                    });
                                }
                            }
                        } catch (Exception e) {
                            Log.v(Constants.TAG, "addPatient() exception: " + e.toString());
                            publishResults("addPatient", STATUS_ERROR, null);
                        } finally {
                            publishResults("addPatient", STATUS_FINISHED, null);
                        }
                    }, (error) -> {
                        // Handle Error
                        Log.e(Constants.TAG, "addPatient Error: " + error.toString());
                        publishResults("addPatient", STATUS_ERROR, null);
                    });

                } catch (Exception e) {
                    Log.e(Constants.TAG, "addPatient() Exception: " + e.toString());
                    publishResults("addPatient", STATUS_ERROR, null);
                }
            }
        }
    }
}