使用 PFImageView 解析 NSInternalInconsistencyException
Parse NSInternalInconsistencyException raises with PFImageView
我现在遇到 Parse 的奇怪行为。
我的应用程序在数周的开发中运行良好,并且自项目设置以来从未提出 'NSInternalInconsistencyException'。
但是我刚刚在我的 ProfileViewController 中实现了一个 PFImageView(UITabBarController 中的第二个 VC,因此仅在用户登录时显示)并且我的应用程序不断崩溃并出现此错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'You have to call setApplicationId:clientKey: on Parse to configure Parse.'
当然,我在 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
:
中检查了我的 Parse 设置
Parse.setApplicationId("myApplicationID", clientKey: "myClientKey")
Parse.enableLocalDatastore()
// This is where I used to activate Parse, but SO and Parse forums kept
// saying you should try to do this at the very beginning
ParseCrashReporting.enable()
PFAnalytics.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
在我的 ProfileViewController 中,这是我从 Parse 加载图像的地方:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
userImageView.file = user["profilePicture"] as PFFile
userImageView.loadInBackground { (image, error) -> Void in
if error != nil {
JSSAlertView().danger(self, title: "Loading Image Failed", text: "Please check your connection")
} else {
self.userImageView.image = image
}
}
}
设置图片实例变量:
var image: UIImage? {
// If User picked a new image, automatically update imageView
didSet {
userImageView.image = image
// Convert image to PNG and save in in Parse-Cloud
let imageData = UIImagePNGRepresentation(image)
let imageFile = PFFile(name: "profilePicture.png", data: imageData)
// Attach this file to our user
user["profilePicture"] = imageFile
user.saveInBackgroundWithBlock { (success, error) -> Void in
if error != nil {
JSSAlertView().danger(self, title: "Failed To Save Image", text: "Please try again saving your image!")
}
}
userImageView.file = user["profilePicture"] as PFFile
}
}
我不知道这些东西之间有什么样的联系,但我没有改变整个项目的任何其他东西。
此致,
好的,经过数小时的反复试验,我终于找到了解决这个问题的方法。
您应该永远不要在视图控制器header 中执行解析调用,因为这些调用可能在API 注册之前执行。
例如:
class ViewController: UIViewController {
// this will crash the app as above!
var user: PFUser! = PFUser.currentUser()
override func viewDidLoad() {
super.viewDidLoad()
// initialize the user here. This will work just fine
user = PFUser.currentUser()
}
}
希望这可以避免您犯同样的错误!
我现在遇到 Parse 的奇怪行为。 我的应用程序在数周的开发中运行良好,并且自项目设置以来从未提出 'NSInternalInconsistencyException'。 但是我刚刚在我的 ProfileViewController 中实现了一个 PFImageView(UITabBarController 中的第二个 VC,因此仅在用户登录时显示)并且我的应用程序不断崩溃并出现此错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'You have to call setApplicationId:clientKey: on Parse to configure Parse.'
当然,我在 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
:
Parse.setApplicationId("myApplicationID", clientKey: "myClientKey")
Parse.enableLocalDatastore()
// This is where I used to activate Parse, but SO and Parse forums kept
// saying you should try to do this at the very beginning
ParseCrashReporting.enable()
PFAnalytics.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
在我的 ProfileViewController 中,这是我从 Parse 加载图像的地方:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
userImageView.file = user["profilePicture"] as PFFile
userImageView.loadInBackground { (image, error) -> Void in
if error != nil {
JSSAlertView().danger(self, title: "Loading Image Failed", text: "Please check your connection")
} else {
self.userImageView.image = image
}
}
}
设置图片实例变量:
var image: UIImage? {
// If User picked a new image, automatically update imageView
didSet {
userImageView.image = image
// Convert image to PNG and save in in Parse-Cloud
let imageData = UIImagePNGRepresentation(image)
let imageFile = PFFile(name: "profilePicture.png", data: imageData)
// Attach this file to our user
user["profilePicture"] = imageFile
user.saveInBackgroundWithBlock { (success, error) -> Void in
if error != nil {
JSSAlertView().danger(self, title: "Failed To Save Image", text: "Please try again saving your image!")
}
}
userImageView.file = user["profilePicture"] as PFFile
}
}
我不知道这些东西之间有什么样的联系,但我没有改变整个项目的任何其他东西。
此致,
好的,经过数小时的反复试验,我终于找到了解决这个问题的方法。 您应该永远不要在视图控制器header 中执行解析调用,因为这些调用可能在API 注册之前执行。
例如:
class ViewController: UIViewController {
// this will crash the app as above!
var user: PFUser! = PFUser.currentUser()
override func viewDidLoad() {
super.viewDidLoad()
// initialize the user here. This will work just fine
user = PFUser.currentUser()
}
}
希望这可以避免您犯同样的错误!