swift - 使用 PFImageView 解析 NSInternalInconsistencyException

标签 swift parse-platform 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
    }

}

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

问候,

最佳答案

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

相关文章:

swift - 使用字符串参数描述类属性

php - 尝试使用 PHP Parse.com 查询日期之间

ios - CollectionView 并不总是正确显示数据

ios - 如何在 Swift 中使用 PFImageView?

ios - Swift 将数组传递给函数并返回

ios - 在 SliderRow 上显示整数值

ios - UIApplicationExitOnSuspend 用于挂起而不是后台

android - 在android中从解析中删除特定行没有成功

ios - Parse.com 中 Pointer<Class> 的问题

ios - 图像未出现在 PFQueryCollectionViewController 中