Vlad Iancu
Vlad Iancu

Reputation: 487

Saving data into Parse.com cloud for another user

I have an app which works with users, and offers the possibility to follow certain users, at the current user choice.

Unfortunately, I don't know how to modify data of a user which is not the current user. I have not seen anything like that stated in the Parse.com docs(or i missed).

I have a column in my database in "Users" class called "usersFollowed" and when the current user clicks "Follow" i want to add the current user to the "usersFollowed" list, but adding them like you will see in the following code does not work.

Follow user code:

dialog = ProgressDialog.show(context, "",
                        "Following...", true);
                viewHolder.userFollow.setSelected(true);
                viewHolder.userFollow.setText("FOLLOWING");

                ParseQuery<ParseUser> userListQuery = ParseUser.getQuery();
                userListQuery.whereEqualTo("screenName", parseUserList.get(position).get("screenName").toString());
                userListQuery.findInBackground(new FindCallback<ParseUser>() {
                    @Override
                    public void done(List<ParseUser> parseObjects, ParseException e) {
                        parseUserFollowedList = new ArrayList<>();

                        if (parseObjects.get(0).getList("usersFollowed") == null) {
                            parseUserFollowedList.add(ParseUser.getCurrentUser());
                            parseObjects.get(0).addAll("usersFollowed", parseUserFollowedList);
                        } else {
                            parseUserFollowedList = parseObjects.get(0).getList("usersFollowed");
                            parseUserFollowedList.add(ParseUser.getCurrentUser());
                            parseObjects.get(0).addAll("usersFollowed", parseUserFollowedList);
                        }
                        parseObjects.get(0).saveInBackground(new SaveCallback() {
                            @Override
                            public void done(ParseException e) {
                                dialog.dismiss();
                            }
                        });
                    }
                });

.saveInBackground does not work because it throws exception: Cannot save info for user that is not logged in".

Can anyone help me in how to do this?

Cheers!

Upvotes: 0

Views: 74

Answers (1)

danh
danh

Reputation: 62676

Users may modify only their own data. But the idea of following can be implemented such that only the currentUser's record requires write access: If Jack chooses to follow Jill, then Jack writes to his "following" relation. This works as long as we don't try to represent "followedBy" in Jill's data (which can be achieved instead with a query).

Or consider that the User table represents the private relationship between a real person and your app. It might make better sense to model the idea of a user's public face with your own custom object, and model following relationships between those. I mention this idea elsewhere here and here.

Upvotes: 1

Related Questions