gpt4 book ai didi

ios - 当应用程序恢复时注销后快速解析应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 13:06:19 25 4
gpt4 key购买 nike

我的代码有一个奇怪的错误,我无法解决。

我已经使用 swift 和 parse 构建了一个应用程序。您登录,然后被带到主页,这是一个表格 View 。现在,如果您已登录,离开并返回应用程序,一切都很好。

但是,当我注销时,用户会被带回登录屏幕。现在,如果用户离开该应用程序,但又回来了,该应用程序会崩溃,并在展开可选值时给我错误 unexpectedly found nil 。

对我来说,可能发生的事情毫无意义。当应用程序从头开始重新启动时,用户设置为 nil,然后用户登录,一切都很酷。当您注销时,用户将被设置回 nil 并转到登录屏幕。如果您恢复该应用程序,则会崩溃。

这是我退出用户的方式的问题吗?

相关代码贴在下面..

在登录页面上:

@IBAction func loginButton(sender: AnyObject) {
//checks if there is a matching username with a matching password. if so, lets the user log in.



PFUser.logInWithUsernameInBackground(username.text, password: userPassword.text) {
(user: PFUser?, error: NSError?) -> Void in
if user != nil {
// Do stuff after successful login.
//go to main table view
//get current user
//display current users locations
println("login success")


//shows home screen after successful login.
self.performSegueWithIdentifier("showHomeFromLogin", sender: self)
} else {
// The login failed. Check error to see why.
//display error?
self.displayAlert("Login Failed", alertMessage: "Double check to make sure the username and password are correct.")
println("login failed")
}
}

}


var activeField: UITextField?





override func viewDidLoad() {
super.viewDidLoad()


username.delegate = self
userPassword.delegate = self

registerForKeyboardNotifications()




// Do any additional setup after loading the view.
//setup so when we tap outside the edit, we close keyboard.
var tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")
view.addGestureRecognizer(tap)
}


override func viewDidAppear(animated: Bool) {
if PFUser.currentUser() != nil {
self.performSegueWithIdentifier("showHomeFromLogin", sender: self)
}

}

在用户登出的首页:

@IBAction func logoutButton(sender: AnyObject) {

PFUser.logOut()
var currentUser = PFUser.currentUser() // this will now be nil
self.performSegueWithIdentifier("logoutSegue", sender: self)
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

NSNotificationCenter.defaultCenter().addObserver(self, selector: "launchSync", name: UIApplicationDidBecomeActiveNotification, object: nil)

//setting table view datasource and delegate.
self.tableView.dataSource = self
self.tableView.delegate = self



var currentUser = PFUser.currentUser()
println(currentUser)

}

我认为在此处中断 query.whereKey("User", equalTo:PFUser.currentUser()!):

func launchSync() {
var query = PFQuery(className:"ParseLighthouse")
query.whereKey("User", equalTo:PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in

if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) lighthouses.")
// Do something with the found objects
if let light = objects as? [PFObject] {
for object in light {
println(object.objectId)
}
}
println(objects?.count)
self.syncToLighthouse(objects!)
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
}

这个 launchsync 函数是在我的主屏幕上的 view did appear 方法中调用的。是否有可能在执行注销 segue 时,主视图 Controller 仍在后台运行,所以当我恢复时,该代码现在无法找到用户,因为它已设置回 nil?

最佳答案

在下一行

NSNotificationCenter.defaultCenter().addObserver(self, selector: "launchSync", name: UIApplicationDidBecomeActiveNotification, object: nil)

你说只要应用程序激活就调用 launchSync 函数。

正如 Paulw11 所提到的,在该函数中,您在 PFUser.currentUser() 上强制解包(使用 !),当用户注销时将返回 nil。

你可以通过确保当前用户不是 nil 来解决这个问题

func launchSync() {
if (PFUser.currentUser() != nil) {
var query = PFQuery(className:"ParseLighthouse")
query.whereKey("User", equalTo:PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in

if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) lighthouses.")
// Do something with the found objects
if let light = objects as? [PFObject] {
for object in light {
println(object.objectId)
}
}
println(objects?.count)
self.syncToLighthouse(objects!)
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
}

关于ios - 当应用程序恢复时注销后快速解析应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32639907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com