Android: Parse beforesave to database

≯℡__Kan透↙ 提交于 2020-01-03 04:47:09

问题


I am going to save records to Parse database.

Saving to Parse:

    ParseObject database = null;
    database = new ParseObject("Record_db");
    database.put("period_ref",  current1_draw_ref_I);
    database.put("remark",          "na");
    database.put("publishing",      "publishing");
    database.saveInBackground();

Cloud Code:

Parse.Cloud.beforeSave("check_duplicate", function(request, response) 
{
    var DB = Parse.Object.extend("Record_db"); 
    var query = new Parse.Query(DB);
        query.equalTo("period_ref", request.object.get("period_ref"));
        query.first
        ({
      success: function(object) 
    {
            if (object) 
        {
                response.error("A Period with this ref already exists.");
            } 
        else
        {
                response.success();
            }
      },
      error: function(error) 
    {
        response.error("Could not validate uniqueness for this period ref object.");
        }
    });  
});

Question:

The records can be saved to the Record_db database. But I do not know how to connect and invoke the "check_duplicate" cloud function for checking duplicate beforeSave. I found there are no tutorials or documentations on such basic operations.

How such beforesave works and when should it be called???

Could you please kindly tell me how to write in the Android code to check duplicate (if duplicate then do not save, if it is new record, then save to Parse DB) and then save to Parse? This basics stuck me for a week which is so desperating...Many thanks for your help in advance.


回答1:


You are very close with your implementation, however, the before*/after* methods require the parameter being the actual classname the code should be run for, not a random method name.

beforeSave,afterSave and beforeDelete,afterDelete, get invoked automatically by Parse once an object of the class defined in the function definition is saved.

So instead of naming the method check_duplicate, use the classname Record_db like so:

Parse.Cloud.beforeSave("Record_db", function(request, response) {
    //... your code ...
});

Also please note that these methods run on every save, not just on object creation, you can use request.object.isNew() to check if the object that gets saved is new or already existed.



来源:https://stackoverflow.com/questions/29060305/android-parse-beforesave-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!