【问题标题】:Parse NSInternalInconsistencyException raises with PFImageView使用 PFImageView 解析 NSInternalInconsistencyException 引发
【发布时间】:2015-03-08 05:15:08
【问题描述】:

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

}

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

问候,

【问题讨论】:

  • 您是否尝试过将图像加载到 viewdidload 中...?
  • 是的,我做了,结果和上面一样。

标签: swift parse-platform pfimageview


【解决方案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()
  }
}

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

【讨论】:

  • 嘿,我有一个关于 JSSAlertView 的问题,如果没有 presentViewContoller 方法,您如何让警报显示在您的 if else 语句中?你是在初始化它们还是?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多