Abel Chua
Abel Chua

Reputation: 40

Can't seem to save multiple objects in parse (Android)

I'm having some issues with saving multiple related objects into the databrowser.

Here i'm trying to create a relation between friends, so i'm creating an Array of "friendsAdded". Currently I can create a one-way relationship of the user adding a friend.

Such as this:

 protected void addfriend(int position) {
    // TODO Auto-generated method stub
    String kliquitId =(((List<KliquitUser>) userlist).get(position).getObjectId());

    ParseQuery<ParseUser> otheruser = ParseUser.getQuery();
    otheruser.getInBackground(kliquitId, new GetCallback<ParseUser>() {
        @Override
        public void done(ParseUser otheruser, ParseException e) {
            // TODO Auto-generated method stub
            ParseUser user = ParseUser.getCurrentUser();
            user.addUnique("friendsAdded", otheruser);
            user.saveInBackground();

}

    });

So Here's the problem...

I want to create a "two-way relationship" whenever an add friend happens...

So, I can't seem to add the current parse user to the array of "friend who was added"..

I tried this.but it doesn't work:

protected void addfriend(int position) {
    // TODO Auto-generated method stub
    String kliquitId =(((List<KliquitUser>) userlist).get(position).getObjectId());

    ParseQuery<ParseUser> otheruser = ParseUser.getQuery();
    otheruser.getInBackground(kliquitId, new GetCallback<ParseUser>() {
        @Override
        public void done(ParseUser otheruser, ParseException e) {
            // TODO Auto-generated method stub
            ParseUser user = ParseUser.getCurrentUser();
            otheruser.addUnique("friendsAdded", user);
            user.addUnique("friendsAdded", otheruser);
            user.saveInBackground();

}

    });

Does anyone have any idea??

Thank you!

Upvotes: 0

Views: 546

Answers (2)

vaibhav
vaibhav

Reputation: 644

 for ( int i = 0; i < imageArrayList.size(); i++) {
                                pos = i;
                                byte[] image = (getByteArray(imageArrayList.get(i)));
                                final ParseFile file = new ParseFile("abc.jpg", image);
                                final int finalPos = pos;
                                file.saveInBackground(new SaveCallback() {
                                    @Override
                                    public void done(ParseException e) {
                                        parseFiles.add(file);
                                        if(finalPos == imageArrayList.size()-1)
                                        {
                                            ParseObject imagelist = new ParseObject("Images");
                                            imagelist.put("refrence", form.getObjectId());
                                            imagelist.put("imagesfile", parseFiles);
                                            imagelist.saveEventually();
                                        }
                                    }
                                });


                            }

Upvotes: 0

user2163298
user2163298

Reputation: 655

The User table is secured by Parse in that you can not modify another User object, you can only modify the currentUser object. From the Parse docs:

"The ParseUser class is secured by default. Data stored in a ParseUser can only be modified by that user. By default, the data can still be read by any client."

Your line otheruser.addUnique("friendsAdded", user); should not be allowed as you are modifying a different User object than the currentUser with this attempt.

I believe the correct approach for you would be to use a Relation instead of an Array. You shoud read up on the Parse docs very thoroughly - a lot of the answers you will seek will be right there in the docs. Read up on relational data: https://parse.com/docs/android_guide#objects-pointers

Upvotes: 1

Related Questions