swiftparse-platformpfimageview

Parse NSInternalInconsistencyException raises with PFImageView


I'm experiencing an odd behaviour of Parse right now. My App worked fine for weeks of development and never raised a 'NSInternalInconsistencyException' since project setup. But I just implemented a PFImageView in my ProfileViewController (second VC in UITabBarController, therefore only showing when user is logged in) and my App keeps crashing with this error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'You have to call setApplicationId:clientKey: on Parse to configure Parse.'

Of course I checked my Parse setup in 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)

In my ProfileViewController this is where I load my image from 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
        }
    }

}

Setting the instance variable of 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
    }

}

I have no clue what kind of connection could be between these things, but I didn't change anything else in the whole project.

Regards,


Solution

  • Ok, finally after hours of trial and error I found the solution for this problem. You should never ever execute parse calls in the header of view controllers, as these calls could be executed before the API registration.

    For example:

    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()
      }
    }
    

    Hope this saves you from having the same errors!