Search code examples
androidparse-platformparse-server

Parse Server - Add Relationship in Android/Java


I am using the Parse Platform as a backend for my android application.

I have a database / class named 'ClassA' which contains a column relation to a class called 'Announcement'

I am trying to create an announcement and add the relation to the ClassA.

I have tired using the API reference on the official website and also code examples found on programcreek

Announcement announcement = new Announcement();
announcement.setTitle(announcements_title.getText().toString());
announcement.setText(announcements_text.getText().toString());

ParseRelation<Announcement> relation = classA.getRelation("announcements");
relation.add(announcement);
classA.saveInBackground(new SaveCallback() {
...
}

I am currently getting the following error message: java.lang.IllegalStateException: unable to encode an association with an unsaved ParseObject

But i have tried to

announcement.saveInBackground();

before creating the relation but that is not working either


Solution

  • Try something like:

    ParseObject announcement = new ParseObject("Announcement");
    announcement.put("title", announcements_title.getText().toString());
    announcement.put("text", announcements_text.getText().toString());
    announcement.saveInBackground(new SaveCallback() {
        ParseRelation<ParseObject> relation = classA.getRelation("announcements");
        relation.add(announcement);
        classA.saveInBackground(new SaveCallback() {
            ...
        });
    });