问题
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