Ondrej Tokar
Ondrej Tokar

Reputation: 5070

How to delete ParseObject if I have only objectID?

I did findInBackground query to get list of objects. After that I create my own objects from results I've gotten:

query.findInBackground(new FindCallback<ParseObject>() {
        public void done(final List<ParseObject> hangOutList, ParseException e) {
            if (e == null) {
                for (int i = 0; i < hangOutList.size(); i++) {
                    String objectID= hangOutList.get(i).getObjectId();
                    String city = hangOutList.get(i).getString("City");
                    String name = hangOutList.get(i).getString("Name");
                    String createdBy = 
                           hangOutList.get(i).getString("CreatedBy");
                    HangOut ho=new HangOut(objectID, name, city, createdBy);
                    hangOuts.add(ho);
                }

Now, when I click on one of them, I want to be able to delete it. In documentation, they say to do this: myObject.deleteInBackground();

However, I don't have ParseObject so I can't do that. Right now I was trying to do something like this:

private void deleteHangout() {
   ParseObject po=new ParseObject("HangOut");
    po.setObjectId(hangOut.getObjectID());
    po.deleteInBackground();
}

It doesn't work. Is there any way how to create Parse object by ID. Or any other idea?

ERROR I get after executing deleteHangout:

01-18 13:41:07.659    1716-1716/com.parse.starter E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Attempted to change an objectId to one that's  
        already known to the Offline Store.
        at com.parse.OfflineStore.updateObjectId(OfflineStore.java:598)
        at com.parse.ParseObject.setObjectIdInternal(ParseObject.java:1106)
        at com.parse.ParseObject.setObjectId(ParseObject.java:1094)
        at com.parse.starter.HangOuts.
           HangOutDetail.deleteHangout(HangOutDetail.java:61)
        at com.parse.starter.HangOuts.
           HangOutDetail.access$000(HangOutDetail.java:16)
        at com.parse.starter.HangOuts.
           HangOutDetail$1.onClick(HangOutDetail.java:54)
        at android.view.View.performClick(View.java:4204)
        at android.view.View$PerformClick.run(View.java:17355)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.
           ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

NOTE: I want to avoid unnecessary calls to server.

Upvotes: 2

Views: 1921

Answers (1)

Kiloreux
Kiloreux

Reputation: 2256

What are you doing is that you are creating an object and then calling the method on another object that you just created , to do what you want you need to create a Parse object without data and then try to use the deleteEventually method so it deletes the object locally first and even if offline and once connected it will delete it from servers .

ParseObject po= ParseObject.createWithoutData("HangOut", hangOut.getObjectID());
po.deleteEventually();

Upvotes: 5

Related Questions