gpt4 book ai didi

swift - 使用 PFImageView 解析 NSInternalInconsistencyException

转载 作者:行者123 更新时间:2023-11-28 07:09:43 24 4
gpt4 key购买 nike

我现在遇到 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
}

}

我不知道这些东西之间可能有什么样的联系,但我没有改变整个项目中的任何其他东西。

问候,

最佳答案

好的,经过几个小时的反复试验,我终于找到了解决这个问题的方法。您应该永远不要在 View Controller 的 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()
}
}

希望这可以避免您犯同样的错误!

关于swift - 使用 PFImageView 解析 NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28923334/

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