【问题标题】:how to correctly make cloud code request with parse?如何通过解析正确地发出云代码请求?
【发布时间】:2015-08-27 19:07:01
【问题描述】:

我正在尝试使用 Parse 独占我的所有用户会话,这意味着如果用户已经在某个位置的某个设备上登录,如果另一个设备使用相同的凭据登录,我想要上一个会话(s ) 被终止,当然还有一条警报视图的消息。有点像旧的 AOL 即时消息格式。我认为这个动作的代码应该写在登录逻辑中,所以我在我的登录“成功”代码中写了这个:

 PFUser.logInWithUsernameInBackground(userName, password: passWord) {
        (user, error: NSError?) -> Void in
        if user != nil || error == nil {
            dispatch_async(dispatch_get_main_queue()) {
                self.performSegueWithIdentifier("loginSuccess", sender: self)

                 PFCloud.callFunctionInBackground("currentUser", withParameters: ["PFUser":"currentUser"])
                    //..... Get other currentUser session tokens and destroy them

            }

        } else {

这可能不是正确的云代码调用,但您明白了。当用户在另一台设备上再次登录时,我想抓取其他会话并终止它们。有谁知道快速提出此请求的正确方法?

【问题讨论】:

    标签: ios swift parse-platform xcode6 usersession


    【解决方案1】:

    我口吃很快,但我想我可以用几乎很快的语速回答。关键思想是只有在云表示可以之后才开始成功继续。这就是我认为你想要的:

    PFUser.logInWithUsernameInBackground(userName, password: passWord) {
        (user, error: NSError?) -> Void in
        if (user != nil) {
            // don't do the segue until we know it's unique login
            // pass no params to the cloud in swift (not sure if [] is the way to say that)
            PFCloud.callFunctionInBackground("isLoginRedundant", withParameters: []) {
                (response: AnyObject?, error: NSError?) -> Void in
                let dictionary = response as! [String:Bool]
                var isRedundant : Bool
                isRedundant = dictionary["isRedundant"]!
                if (isRedundant) {
                    // I think you can adequately undo everything about the login by logging out
                    PFUser.logOutInBackgroundWithBlock() { (error: NSError?) -> Void in
                        // update the UI to say, login rejected because you're logged in elsewhere
                        // maybe do a segue here?
                    }
                } else {
                    // good login and non-redundant, do the segue
                    self.performSegueWithIdentifier("loginSuccess", sender: self)
                }
            }
        } else {
            // login failed for typical reasons, update the UI
        }
    } 
    

    请不要对 swift 语法太认真。这个想法是将segue嵌套在完成处理程序中,以知道您需要在启动它之前执行它。另外,请注意,完成处理程序中 main_queue 上的显式放置是不必要的。 SDK 在 main 上运行这些块。

    确定用户会话是否冗余(不是唯一)的简单检查如下所示...

    Parse.Cloud.define("isLoginRedundant", function(request, response) {
        var sessionQuery = new Parse.Query(Parse.Session);
        sessionQuery.equalTo("user", request.user);
        sessionQuery.find().then(function(sessions) {
            response.success( { isRedundant: sessions.length>1 } );
        }, function(error) {
            response.error(error);
        });
    });
    

    【讨论】:

    • 好的,我明白了,所以在您的回答中,我应该编写自己的自定义代码的唯一地方是在 logoutInBackground 之后?除了那基本上我应该正确滚动的结构吗?
    • 是的。在那里,在注销完成后,在其他条件下的叶级别(cmets 说“更新 ui”)
    • 所以这只会停止过度登录而不是终止当前会话正确吗?
    • 登录创建一个会话,而注销则相反(销毁它)。我想这就是你想要的,对吧?
    • 是的,但我想确保它只是破坏“多余”会话,而不是原始会话。你明白吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 2017-11-19
    • 2022-10-02
    • 2011-08-29
    • 2016-09-07
    • 2017-09-02
    相关资源
    最近更新 更多