Ios 如何使用parse正确发出云代码请求?
我正在尝试使用Parse将我的所有用户会话设置为独占,这意味着如果某个用户已登录到某个位置的某个设备,如果另一个设备使用相同的凭据登录,我希望前一个会话终止,当然会显示一条警报视图消息。有点像旧的AOL即时消息格式。我认为这个操作的代码应该写在登录逻辑中,所以我在我的登录“继任者”代码中写了这个:Ios 如何使用parse正确发出云代码请求?,ios,swift,parse-platform,xcode6,usersession,Ios,Swift,Parse Platform,Xcode6,Usersession,我正在尝试使用Parse将我的所有用户会话设置为独占,这意味着如果某个用户已登录到某个位置的某个设备,如果另一个设备使用相同的凭据登录,我希望前一个会话终止,当然会显示一条警报视图消息。有点像旧的AOL即时消息格式。我认为这个操作的代码应该写在登录逻辑中,所以我在我的登录“继任者”代码中写了这个: 这可能不是正确的云代码调用,但你明白了。当用户再次登录到另一台设备时,我想抓取其他会话并终止它们。有人知道用swift提出这个请求的正确方法吗?我口吃地说swift,但我想我能用几乎swift的语言回
这可能不是正确的云代码调用,但你明白了。当用户再次登录到另一台设备时,我想抓取其他会话并终止它们。有人知道用swift提出这个请求的正确方法吗?我口吃地说swift,但我想我能用几乎swift的语言回答。关键的想法是,只有在云表示一切正常后,才能开始成功之旅。以下是我认为你想要的:
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嵌套在完成处理程序中,以便在启动它之前知道需要这样做。另外,请注意,在完成处理程序中主_队列上的显式放置是不必要的。SDK在主服务器上运行这些块
确定用户会话是否冗余(非唯一)的简单检查如下所示
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);
});
});
好的,我明白了,在你的回答中,我应该在后台写我自己的自定义代码的唯一地方是?除此之外,这基本上是我应该采用的结构,对吗?是的。在那里,在注销完成后,在叶级别的其他条件下(注释说“更新ui”),这将只是停止多余的登录,而不是终止当前会话。正确吗?登录创建一个会话,注销则相反(销毁它)。我想这就是你想要的,对吧?是的,但我想确保它只会破坏“多余”的会话,而不是原始会话。你明白吗?
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);
});
});