如果我只有objectID,如何删除ParseObject?

时间:2015-01-18 13:51:48

标签: android parse-platform

我做了findInBackground查询以获取对象列表。之后,我从我得到的结果中创建自己的对象:

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);
                }

现在,当我点击其中一个时,我希望能够将其删除。在文档中,他们说要执行此操作:myObject.deleteInBackground();

但是,我没有ParseObject所以我不能这样做。现在我正试图做这样的事情:

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

它不起作用。有没有办法如何通过ID创建Parse对象。还是其他任何想法?

执行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)

注意:我想避免对服务器进行不必要的调用。

1 个答案:

答案 0 :(得分:5)

你在做什么是你正在创建一个对象,然后在你刚刚创建的另一个对象上调用该方法,做你想要的创建一个没有data的Parse对象所需的东西,然后尝试使用deleteEventually方法,因此它首先在本地删除对象,即使脱机,一旦连接,它将从服务器中删除它。

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