我正在尝试使我的所有用户 session 都与 Parse 独占,这意味着如果用户已经在某个位置的某个设备上登录,如果另一个设备使用相同的凭据登录,我想要以前的 session ) 被终止,当然还有一条警告 View 的消息。有点像旧的 AOL Instant Messaging 格式。我想这个 Action 的代码应该写在登录逻辑中,所以我在我的登录“继承”代码中写了这个:
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 {
这可能不是正确的云代码调用,但您明白了。当用户在另一台设备上再次登录时,我想捕获其他 session 并终止它们。有谁知道快速发出此请求的正确方法?
最佳答案
我说话很快,但有点结巴,但我想我几乎可以很快地回答。关键思想是只有在云表示没问题后才开始成功转场。这就是我认为您想要的:
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 在主节点上运行这些 block 。
确定用户 session 是否冗余(不唯一)的简单检查如下所示...
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);
});
});
关于ios - 如何使用解析正确地发出云代码请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32257589/