每次卸载和安装应用程序时,都会获得一个新的安装对象。不幸的是,旧的 Installation 对象永远不会消失。您需要做的是为设备使用一些唯一标识符,如果存在,只需更新安装。
例如,假设您尝试在 Android 上执行此操作。
在您的应用中,您可以获取设备的ANDROID_ID 并将其保存到安装中:`
ParseInstallation.getCurrentInstallation().put("uniqueId",
Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID));
那么你就可以拥有一个每次保存新安装时触发的云功能:
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.Installation);
query.equalTo("uniqueId", request.object.get("uniqueId"));
query.first().then(function(duplicate) {
if (typeof duplicate === "undefined") {
console.log("Duplicate does not exist,New installation");
response.success();
} else {
console.log("Duplicate exist..Trying to delete " + duplicate.id);
duplicate.destroy().then(function(duplicate) {
console.log("Successfully deleted duplicate");
response.success();
}, function() {
console.log(error.code + " " + error.message);
response.success();
});
}
}, function(error) {
console.warn(error.code + error.message);
response.success();
});
})
附:那个函数我用了很久了,不记得从哪里弄来的了,但不是我自己写的。