androidparse-platformparse-android-sdk

When to use a pointer and when to use ParseRelation in this example


I'm struggling with keeping things straight for a chatRoom example I'm working on.

Given: UserA UserB ChatRoom01

In this example when UserA enters ChatRoom01, I want to create an association between the two. Now since ChatRoom01 already exists, I can grab the object by doing:

ParseQuery<ParseObject> query = ParseQuery.getQuery("ChatRoom");
        query.getInBackground(getIntent().getStringExtra("chatRoom"), new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject chatRoom, ParseException e) {
                if(e == null){
                    // we found the object
                    myChatRoom = chatRoom;
                    // set the activity's title
                    setTitle(myChatRoom.getString("name"));
                }else{
                    // aww sh!t. can't find it
                }
            }
        });

If I were to use a pointer, I would do something like:

myChatRoom.put("member", ParseUser.getCurrentUser());

When the user leaves the room, I'd like to remove the association. I'm thinking that:

myChatRoom.remove("member");

would remove the member association from the room. That's not what I want to do. How would I accomplish this?

If it turns out that pointers are the ideal solution here, when UserB enters the chatRoom, I'll follow the same steps as when UserA entered the room. But by doing that, wouldn't I overwrite UserA's member information with UserB? ParseRelation seems like it would be a better choice because I can add/remove users; however, since there could be in excess of 100 users, I get the impression that pointers are the way to go. I'm open to suggestions on how to handle this.

The alternative would be to subscribe them to a channel upon entry, and unsubscribe them when they leave. Which sounds great, but I'm unsure on how to check if they are already subscribed.


Solution

  • It sounds like you want to use ParseRelation.

    To add to a relation use:

    ParseRelation<ParseObject> relation = myChatRoom.getRelation("members");
    relation.add(ParseUser.getCurrentUser());
    

    To remove a relation use:

    relation.remove(ParseUser.getCurrentUser());
    

    Documentation:

    Relations Guide

    API Reference