尝试使用Parse更新对象时出现IllegalArgumentException

时间:2022-09-25 18:48:32

Using Parse.com, I want to retrieve an object from the local DB, update a field and save it back to the Parse Cloud... here is my code so far:

使用Parse.com,我想从本地数据库中检索一个对象,更新一个字段并将其保存回Parse Cloud ...这是我的代码到目前为止:

String objId = getIntent().getStringExtra("EXTRA_OBJ_ID");

ParseQuery<ParseObject> query = ParseQuery.getQuery("someTable");
query.fromLocalDatastore();
query.getInBackground(objId, new GetCallback<ParseObject>() {
    @Override
    public void done(ParseObject parseObject, ParseException e) {
        if (e == null) {
            Date expiry = parseObject.getDate("expiryDate");
            long millis = expiry.getTime();
            Date current = new Date();
            parseObject.add("expiryDate", current);
            parseObject.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        //success
                    } else {
                        Log.e(TAG, e.toString());
                    }
                }
            });
        } else {
            Log.e(TAG, e.toString());
        }
    }
});

but at parseObject.add("expiryDate", current); I get the following exception:

但是在parseObject.add(“expiryDate”,当前);我得到以下异常:

java.lang.IllegalArgumentException: Operation is invalid after previous operation.
        at com.parse.ParseAddOperation.apply(ParseAddOperation.java:71)
        at com.parse.ParseObject.performOperation(ParseObject.java:2725)
        at com.parse.ParseObject.addAll(ParseObject.java:2813)
        at com.parse.ParseObject.add(ParseObject.java:2799)
        at com.app.ConversationActivity$8.done(ConversationActivity.java:297)
        at com.app.ConversationActivity$8.done(ConversationActivity.java:283)
        at com.parse.Parse$5$1.run(Parse.java:924)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:212)
        at android.app.ActivityThread.main(ActivityThread.java:5151)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
        at dalvik.system.NativeStart.main(Native Method)

My code is almost copy-pasted from Parse docs, except that I'm querying from local datastore... so how can I make this work? Thanks very much!

我的代码几乎是从Parse文档中复制粘贴的,除了我从本地数据存储区查询...所以我怎样才能使这个工作?非常感谢!

1 个解决方案

#1


Do not do the following: parseObject.add("expiryDate", current);

不要执行以下操作:parseObject.add(“expiryDate”,current);

instead, do this: parseObject.put("expiryDate", current);

相反,这样做:parseObject.put(“expiryDate”,current);

From Parse's documentation

来自Parse的文档

add(String key, Object value)Atomically adds an object to the end of the array associated with a given key.

add(String key,Object value)以原子方式将对象添加到与给定键关联的数组的末尾。

"expiryDate" is not an array it is a Date field for your ParseObject in "someTable". So you just want to update it's value via a call to put, rather than add.

“expiryDate”不是数组,它是“someTable”中ParseObject的Date字段。所以你只想通过调用put来更新它的值,而不是添加。

The put operation is what you are looking for, and should work.

put操作是你正在寻找的,应该工作。

Side Note: You may want to use saveEventually() in order to save to the cloud, and also to the local datastore as well. Since it seems you are trying to take your Parse application offline as well.

附注:您可能希望使用saveEventually()以便保存到云,也可以保存到本地数据存储区。因为您似乎也在试图使您的Parse应用程序脱机。

#1


Do not do the following: parseObject.add("expiryDate", current);

不要执行以下操作:parseObject.add(“expiryDate”,current);

instead, do this: parseObject.put("expiryDate", current);

相反,这样做:parseObject.put(“expiryDate”,current);

From Parse's documentation

来自Parse的文档

add(String key, Object value)Atomically adds an object to the end of the array associated with a given key.

add(String key,Object value)以原子方式将对象添加到与给定键关联的数组的末尾。

"expiryDate" is not an array it is a Date field for your ParseObject in "someTable". So you just want to update it's value via a call to put, rather than add.

“expiryDate”不是数组,它是“someTable”中ParseObject的Date字段。所以你只想通过调用put来更新它的值,而不是添加。

The put operation is what you are looking for, and should work.

put操作是你正在寻找的,应该工作。

Side Note: You may want to use saveEventually() in order to save to the cloud, and also to the local datastore as well. Since it seems you are trying to take your Parse application offline as well.

附注:您可能希望使用saveEventually()以便保存到云,也可以保存到本地数据存储区。因为您似乎也在试图使您的Parse应用程序脱机。