简体   繁体   English

Parse.com:保存时获取唯一号码

[英]Parse.com: Get unique number when saving

I use the Parse.com Cloud service in my Android app to sync data between devices. 我在Android应用中使用Parse.com Cloud服务在设备之间同步数据。

I use the app mainly offline and use the local data store. 我主要使用应用程序离线并使用本地数据存储。

There is one class called Point that has a unique number as identifier I want to display. 有一个名为Point类,它具有唯一的编号作为我想要显示的标识符。 So when working offline I want to create a Point as draft (with a draft text as number) and when synchronizing I want it to get the real number that is unique over all the devices. 因此,当离线工作时,我想创建一个Point作为草稿(草稿文本作为数字),并且在同步时我希望它获得所有设备上唯一的实数。

How would I set the number when saving? 保存时如何设置号码? I was thinking about adding a WebHook in the cloud when saving the Point and giving it a unique number and then in my app use 当我保存Point并给它一个唯一的号码然后在我的app中使用时,我正在考虑在云中添加WebHook

newPoint.saveEventually(new SaveCallback() {            
        public void done(ParseException e) {
            //query for the number
        }
    });

to query the point from the cloud to get the number after it has been saved. 从云中查询点以获取保存后的数字。 But this seems kind of too complicated for such a simple requirement. 但这对于这样一个简单的要求来说似乎太复杂了。 And I am not sure if the SaveCallback() is always triggered when saving it. 而且我不确定SaveCallback()在保存时是否总是被触发。

I would recommend using an afterSave trigger on the Point class to set the unique identifier when the object is newly created. 我建议在Point类上使用afterSave触发器来在新创建对象时设置唯一标识符。 Then, as you've mentioned, you'll need to fetch the value before displaying it. 然后,正如您所提到的,您需要在显示之前获取值。

Here's what the cloud code could look like: 以下是云代码的外观:

// Assign the Point a unique identifier on creation
Parse.Cloud.afterSave("Point", function(request) {

    // Check if the Point is new
    if (!(request.object.existed())) {

        // Get the unique identifier
        var uniqueIdentifier = ...

        // Set the unique identifier
        request.object.set('uniqueIdentifier', uniqueIdentifier);
    }
});

One important bit of information to keep in mind about using saveEventually with SaveCallback() is: 使用saveEventually with SaveCallback()要记住的一个重要信息是:

The callback will only be called if the operation completes within the current app lifecycle. 仅当操作在当前应用程序生命周期内完成时,才会调用回调。 If the app is closed, the callback can't be called, even if the save eventually is completed. 如果应用程序已关闭,则无法调用回调,即使最终完成保存也是如此。

Source: Hector Ramos 来源:赫克托拉莫斯

If the unique identifier should be immediately displayed in the app or if the callback needs to be handled consistently, it would probably be best to use saveInBackground rather than saveEventually . 如果应该立即在应用程序中显示唯一标识符,或者如果需要一致地处理回调,则最好使用saveInBackground而不是saveEventually

Another option would be to dynamically change the callback depending on network availability and/or offline settings. 另一种选择是根据网络可用性和/或离线设置动态更改回调。 For example, if the offline mode is used anytime when the cell signal or wifi is unavailable, then network reachability could be used to check for a network and then use saveInBackground or saveEventually as appropriate. 例如,如果在单元信号或wifi不可用时随时使用离线模式,则可以使用网络可达性来检查网络,然后根据需要使用saveInBackgroundsaveEventually

Update 更新

OP ended up using this code: OP最终使用此代码:

Parse.Cloud.beforeSave("Point", function(request, response) {
    if (!(request.object.existed())) {
        var query = new Parse.Query("Point");
        query.addDescending("uniqueIdentifier");
        query.first({
            success: function(result) {             
                var maxId = result.get("uniqueIdentifier");             
                request.object.set("uniqueIdentifier", maxId + 1);                  
            },
            error: function() {                     
            }
        });            
    }
    response.success();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM