stanley santoso
stanley santoso

Reputation: 343

How to delete a newly created object from parse?

I want to delete an object that I just created. I am using parse to take care all of my data

The way this works is: 1. There is a listView filled with posts. 2. The post has a "like button" 3. when "like" is pressed, a new object will be created that is pointed to the post as the parent. the "like" button will also get hidden and another button called "liked" will be visible. 4. when "liked" button is pressed, the like object that has just been created needs to be deleted. and then the "liked" button will get hidden and the "like" button shows up.

I believe the logic is pretty straight forward. I ave successfully build until part 3. I am currently needing help to delete the like object that I just created.

This is the code of those 2 buttons:

holder.LikeMain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final ParseObject socialObject = mSocial.get(position);
            String objectId = socialObject.getObjectId();
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Social");
            query.getInBackground(objectId, new GetCallback<ParseObject>() {
                public void done(ParseObject reminderObject, ParseException e) {
                    if (e == null) {
                        // Now let's update it with some new data. In this case, only cheatMode and score
                        // will get sent to the Parse Cloud. playerName hasn't changed.


                        ParseObject myLike = new ParseObject("LikedSocial");
                        ParseUser currentUser = ParseUser.getCurrentUser();

                        final String currentUserUsername = currentUser.getUsername();
                        myLike.put("LikeOwner", currentUserUsername);

                        // Add a relation between the Like and Comment
                        myLike.put("PostId", socialObject);

                        // This will save both myLike and myComment
                        myLike.saveInBackground();


                        holder.LikeMain.setVisibility(View.GONE);
                        holder.LikedMain.setVisibility(View.VISIBLE);
                    }
                }
            });
        }
    });
    holder.LikedMain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            ParseObject.createWithoutData("LikedSocial", currentUserUsername).deleteEventually();
            holder.LikeMain.setVisibility(View.VISIBLE);
            holder.LikedMain.setVisibility(View.GONE);
        }
    });

Upvotes: 0

Views: 47

Answers (1)

Ibrahim
Ibrahim

Reputation: 36

You should replace the currentUserName with the object id of the Like in your deletion code. i.e

ParseObject.createWithoutData("LikedSocial", currentUserUsername).deleteEventually();

should be:

ParseObject.createWithoutData("LikedSocial", idOfLikeObject).deleteEventually();

There are two ways to delete objects:

If you have the the object itself in hand, use

myLike.deleteInBackground();

If you only have the ID of the object i.e the ObjectId, use

ParseObject.createWithoutData("LikeSocial", ID).deleteEventually();

Edit:

Also, the way your code is pasted makes it seem like you're setting the OnClickListener twice - first time for setting liked and second time for deleting likes.

If you're doing it that way, it'll probably be more effective to use a variable to indicate the state of your like and use a switch statement to do the necessary 'add like' or 'delete like' in a single OnClickListener.

Upvotes: 1

Related Questions