Search code examples
androidmultithreadingrealmrealm-mobile-platform

Realm Query not updating android


I am getting error when updating the query.This is the error I am getting.

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

This is code I am using in 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);
            }
        }
    }
}

Please check where I am going wrong. I am unable to save the details in query


Solution

  • 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);
                    }
                }
            }
        }
    }