Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Realm - Updating RealmList triggers IllegalArgumentException

I'm attempting to update an existing RealmObject (IncidentCard) which include a RealmList of type IncidentPhoto. The object is updated without any problems as long as I don't attempt to update the RealmList, when I include the list I get the following error message:

E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.trollvik.android.incidents247, PID: 31923
E/AndroidRuntime: java.lang.IllegalArgumentException: Each element of 'value' must be a valid managed object.
E/AndroidRuntime:     at io.realm.IncidentCardRealmProxy.setPhotos(IncidentCardRealmProxy.java:218)
E/AndroidRuntime:     at com.trollvik.android.incidents247.activities.EditCardActivity.saveIncidentCard(EditCardActivity.java:155)
E/AndroidRuntime:     at com.trollvik.android.incidents247.activities.EditCardActivity$1.onClick(EditCardActivity.java:95)
E/AndroidRuntime:     at android.view.View.performClick(View.java:5197)
E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:20926)
E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5944)
E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

This is the IncidentCard class:

public class IncidentCard extends RealmObject {

    @PrimaryKey
    private long id;

    private String timestamp;
    private String type;
    private RealmList<IncidentPhoto> photos;

    public IncidentCard() {

    }

    public IncidentCard(long id, String timestamp, String type){
    this.id = id;
    this.timestamp = timestamp;
    this.type = type;
    }

    public IncidentCard(long id, String timestamp, String type, RealmList<IncidentPhoto> photos){
    this.id = id;
    this.timestamp = timestamp;
    this.type = type;
    this.photos = photos;
    }

    public long getId() {
    return this.id;
    }

    public void setId(long id) {
    this.id = id;
    }

    public String getTimestamp(){
    return this.timestamp;
    }

    public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
    }

    public String getType(){
    return this.type;
    }

    public void setType(String type){
    this.type = type;
    }

    public RealmList<IncidentPhoto> getPhotos() {
    return this.photos;
    }

    public void setPhotos(RealmList<IncidentPhoto> photos) {
    this.photos = photos;
    }
}

This is the IncidentPhoto class:

public class IncidentPhoto extends RealmObject {

    private String photoPath;

    public IncidentPhoto() {

    }

    public IncidentPhoto(String photoPath) {
    this.photoPath = photoPath;
    }

    public String getPhotoPath(){
    return this.photoPath;
    }

    public void setPhotoPath(String photoPath){
    this.photoPath = photoPath;
    }
}

To query the Realm DB I created this helper class:

public class IncidentDbHelper {

    private Realm realm;

    public IncidentDbHelper(Context context) {
    realm = Realm.getInstance(context);
    }

    public void setObject(IncidentCard incidentCard) {
    realm.beginTransaction();
    IncidentCard incident = realm.copyToRealmOrUpdate(incidentCard);
    realm.commitTransaction();
    }

    public IncidentCard getObject(Long id) {
    return realm.where(IncidentCard.class).equalTo("id", id).findFirst();
    }

    public void close(){
    if (realm != null) {
        realm.close();
    }
    }
}

When I add a new incident card I call this activity:

public class NewCardActivity extends AppCompatActivity {

    private static final int REQUEST_IMAGE_CAPTURE = 1;
    private static final String INSTANCE_STATE = "currentPhotoPath";
    private static final String INSTANCE_STATE_LIST = "currentPhotoList";

    private Context mContext;

    private IncidentCard mIncidentCard;

    private IncidentDbHelper mDbHelper;
    private IncidentCardId mIncidentId;
    private IncidentCardTimestamp mIncidentTimestamp;
    private RealmConverter mRealmConverter;

    private Resources mRes;
    private PhotoPath mPath;

    private String mCurrentPhotoPath;
    private ArrayList<String> mCurrentPhotoList;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContext = this;
    mDbHelper = new IncidentDbHelper(mContext);
    mIncidentCard = new IncidentCard();
    mRealmConverter = new RealmConverter();
    mCurrentPhotoList = new ArrayList<String>();
    mIncidentId = new IncidentCardId();
    mIncidentTimestamp = new IncidentCardTimestamp();
    mRes = getResources();
    mPath = new PhotoPath();

    // If savedInstanceState is empty, ignore this code.
    if(savedInstanceState != null){
        mCurrentPhotoPath = savedInstanceState.getString(INSTANCE_STATE);
        mCurrentPhotoList = savedInstanceState.getStringArrayList(INSTANCE_STATE_LIST);
    }
    }

    protected void saveIncidentCard(){
    Realm realm = Realm.getInstance(this);
    Spinner spinner = (Spinner) findViewById(R.id.content_new_card_type);
    String incidentType = spinner.getSelectedItem().toString();

    realm.beginTransaction();
    mIncidentCard.setId(mIncidentId.getNewId());
    mIncidentCard.setTimestamp(mIncidentTimestamp.getNewTimestamp());
    mIncidentCard.setType(incidentType);
    mIncidentCard.setPhotos(mRealmConverter.toRealmList(mCurrentPhotoList));
    realm.commitTransaction();

    mDbHelper.setObject(mIncidentCard);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_new_card, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_new_photo:
            dispatchTakePictureIntent();

        default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            return super.onOptionsItemSelected(item);

    }
    }


    private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File imageFile = null;

    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        try {
            imageFile = mPath.createImageFile();
            mCurrentPhotoPath = imageFile.getAbsolutePath();
        } catch (java.io.IOException e) {
            Log.e(TAG, e.toString());
        }

        // Continue only if the File was successfully created
        if (imageFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(imageFile.getAbsoluteFile()));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString(INSTANCE_STATE, mCurrentPhotoPath);
    savedInstanceState.putStringArrayList(INSTANCE_STATE_LIST, mCurrentPhotoList);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        mCurrentPhotoList.add(mCurrentPhotoPath);
    }
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    mDbHelper.close();
    }
}

While I capture new photos I save the paths of previously captured photos in a ArrayList of type String. Before I set the list of paths to the IncidentCard object I convert the list to a RealmList. This part seems to be working fine.

The problem occurs after I try to save an existing object in EditCardActivity:

public class EditCardActivity extends AppCompatActivity {

    private static final String INTENT_EXTRA = "EXTRA_INCIDENT_ID";
    private static final int REQUEST_IMAGE_CAPTURE = 2;
    private static final String INSTANCE_STATE = "currentPhotoPath";
    private static final String INSTANCE_STATE_LIST = "currentPhotoList";

    private Context mContext;
    private Long mIncidentId;
    private IncidentCard mIncidentCard;
    private IncidentDbHelper mDbHelper;

    private IncidentCardTimestamp mIncidentTimestamp;
    private RealmConverter mRealmConverter;

    private Resources mRes;
    private PhotoPath mPath;

    Spinner mSpinnerType;
    private String mCurrentPhotoPath;
    private ArrayList<String> mCurrentPhotoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mRealmConverter = new RealmConverter();

    mContext = this;
    mDbHelper = new IncidentDbHelper(mContext);
    mIncidentCard = new IncidentCard();
    mCurrentPhotoList = new ArrayList<String>();
    mIncidentTimestamp = new IncidentCardTimestamp();
    mRes = getResources();
    mPath = new PhotoPath();

    // If savedInstanceState is empty, ignore this code.
    if(savedInstanceState != null){
        mCurrentPhotoPath = savedInstanceState.getString(INSTANCE_STATE);
        mCurrentPhotoList = savedInstanceState.getStringArrayList(INSTANCE_STATE_LIST);
    }

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            saveIncidentCard();
            finish();
        }
    });

    // Get Incident ID passed from Main Activity
    Intent intent = getIntent();
    mIncidentId = intent.getLongExtra(INTENT_EXTRA, 0);

    mIncidentCard = mDbHelper.getObject(mIncidentId);

    mTextViewId = (TextView) findViewById(R.id.content_edit_card_id);
    mSpinnerType = (Spinner) findViewById(R.id.content_edit_card_type);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, items);
    mSpinnerType.setAdapter(adapter);
    String compareValue = mIncidentCard.getType();
    if (!compareValue.equals(null)) {
        int spinnerPosition = adapter.getPosition(compareValue);
        mSpinnerType.setSelection(spinnerPosition);
    }

    mCurrentPhotoList = mRealmConverter.toArrayList(mIncidentCard.getPhotos());
    }

    protected void saveIncidentCard(){
    Realm realm = Realm.getInstance(this);
    Spinner spinner = (Spinner) findViewById(R.id.content_edit_card_type);

    String incidentType = spinner.getSelectedItem().toString();

    realm.beginTransaction();
    mIncidentCard.setTimestamp(mIncidentTimestamp.getNewTimestamp());
    mIncidentCard.setType(incidentType);
    mIncidentCard.setPhotos(mRealmConverter.toRealmList(mCurrentPhotoList));
    realm.commitTransaction();

    mDbHelper.setObject(mIncidentCard);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_edit_card, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_edit_photo:
            dispatchTakePictureIntent();

        default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            return super.onOptionsItemSelected(item);

    }
    }


    private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File imageFile = null;

    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        try {
            imageFile = mPath.createImageFile();
            mCurrentPhotoPath = imageFile.getAbsolutePath();
        } catch (java.io.IOException e) {
            Log.e(TAG, e.toString());
        }

        // Continue only if the File was successfully created
        if (imageFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(imageFile.getAbsoluteFile()));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString(INSTANCE_STATE, mCurrentPhotoPath);
    savedInstanceState.putStringArrayList(INSTANCE_STATE_LIST, mCurrentPhotoList);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        mCurrentPhotoList.add(mCurrentPhotoPath);
    }
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    mDbHelper.close();
    }
}

So, if I comment out mIncidentCard.setPhotos() everything seems to work fine, but when I try to set the photos to the IncidentCard object the IllegalArgumentException is triggered.

This is the method I created for converting ArrayLists to RealmLists:

public RealmList<IncidentPhoto> toRealmList(ArrayList<String> arrayList){
    mRealmList = new RealmList<IncidentPhoto>();
    for (int i = 0; i < arrayList.size(); i++){
        IncidentPhoto incidentPhoto = new IncidentPhoto();
        incidentPhoto.setPhotoPath(arrayList.get(i));
        mRealmList.add(incidentPhoto);
    }
    return mRealmList;
}

I've been struggling with this for a while now and I don't understand what I'm doing wrong, so any help will be greatly appreciated.

Realm Exception 'value' is not a valid managed object Realm Java Doc

like image 591
Oyvind Fredstie Avatar asked Jan 06 '23 15:01

Oyvind Fredstie


1 Answers

The problem is, when calling setters to set a RealmList, every element in the list has to be managed by Realm already.

Similar question here Adding standalone-objects to a RealmList

You can modify toRealmList as below:

public RealmList<IncidentPhoto> toRealmList(Realm realm, ArrayList<String> arrayList) {
    mRealmList = new RealmList<IncidentPhoto>();
    for (int i = 0; i < arrayList.size(); i++){
        // Create a IncidentPhoto object which is managed by Realm.
        IncidentPhoto incidentPhoto = realm.createObject(IncidentPhoto.class);
        incidentPhoto.setPhotoPath(arrayList.get(i));
        mRealmList.add(incidentPhoto);
    }
    return mRealmList;
}

or

public RealmList<IncidentPhoto> toRealmList(Realm realm, ArrayList<String> arrayList) {
    mRealmList = new RealmList<IncidentPhoto>();
    for (int i = 0; i < arrayList.size(); i++){
        IncidentPhoto incidentPhoto = new IncidentPhoto();
        incidentPhoto.setPhotoPath(arrayList.get(i));
        // Copy the standalone object to Realm, and get the returned object which is managed by Realm.
        incidentPhoto = realm.copyToRealm(incidentPhoto);
        mRealmList.add(incidentPhoto);
    }
    return mRealmList;
}
like image 169
beeender Avatar answered Feb 03 '23 08:02

beeender