Parse NSInternalInconsistencyException使用PFImageView引发

时间:2015-03-08 05:15:08

标签: swift parse-platform pfimageview

我现在正在经历Parse的奇怪行为。 我的应用程序在数周的开发中运行良好,并且从未提出过“内部不一致异常”#93;自项目设置。 但我刚刚在我的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
    }

}

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

此致

1 个答案:

答案 0 :(得分:1)

好的,经过几个小时的试验和错误,我找到了解决这个问题的方法。 你应该从不在视图控制器的标题中执行解析调用,因为这些调用可以在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()
  }
}

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