简体   繁体   English

使用parse.com在后台注销

[英]Logout in background with parse.com

I am new to creating apps and am trying to make an app that uses parse.com for logging in and out using Xcode 7 and Swift. 我是创建应用程序的新手,正在尝试制作一个使用parse.com使用Xcode 7和Swift登录和注销的应用程序。 I have so far made sign up and login screens and a home screen with, so far, only a sign out button. 到目前为止,我已经完成了注册和登录屏幕以及一个主屏幕,到目前为止,只有一个退出按钮。 I have an IBAction method called "signOut" that is triggered when the sign out button is pressed. 我有一个称为“ signOut”的IBAction方法,当按下退出按钮时会触发该方法。 Inside it I have just two lines of code: 在其中,我只有两行代码:

self.performSegueWithIdentifier("segue that goes back to login screen", sender: self)
PFUser.logOutInBackground()

This works fine when connected to the internet, but when offline the screen freezes for 10-20 seconds while the app tries to connect to the network and then switches back to the login screen. 当连接到Internet时,此方法工作正常,但在脱机状态下,该屏幕冻结10-20秒,而该应用程序尝试连接至网络,然后切换回登录屏幕。 I want it to switch to the login screen and then logout and I do not understand why it does not. 我希望它切换到登录屏幕,然后注销,但我不明白为什么不这样做。 I have even tried moving PFUser.logOutInBackground() to the viewWillAppear and the viewDidLoad methods of my login screen's view controller and still had the same problem. 我什至尝试将PFUser.logOutInBackground()移至登录屏幕的视图控制器的viewWillAppear和viewDidLoad方法,但仍然遇到相同的问题。 Perhaps what confuses me most about this is that any code after the logout command continues to be executed while the app tries to connect to the network even though the view does not change. 也许令我最困惑的是,即使视图未更改,在应用程序尝试连接到网络时,注销命令之后的所有代码仍将继续执行。 Any help with this issue would be greatly appreciated. 任何与此问题的帮助将不胜感激。 Thanks. 谢谢。

Try this: 尝试这个:

PFUser.logOutInBackgroundWithBlock({ (error: NSError?) -> Void in
    if let error = error {
        // Handle error
    } else {
        self.performSegueWithIdentifier("unwindToLoginViewController", sender: self)
    }
})

You need to make sure that performSegueWithIdentifier is run on the main thread which is responsible for updating the UI 您需要确保performSegueWithIdentifier在负责更新UI的主线程上运行

   dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("segue that goes back to login screen", sender: self)
}

PFUser.logOutInBackground()

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

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