0

I've been killing myself for the past 48 hours trying to figure out what I changed and its driving me nuts!

I'm using a Parse database and I have 2 classes:

  • User class which is a subclass of PFUser
  • A Car class which is a subclass of PFObject

This Car class has a field "owner" which is a pointer to the User

Randomly 2 days ago I started getting this error when trying to save a new car or access and existing Car:

2016-01-14 20:18:04.135 YetiTest[3439:1130906] [Error]: Caught "NSInternalInconsistencyException" with reason "Key "firstName" has no data. Call fetchIfNeeded before getting its value.":

firstName is a property of my User class but its populated. Today I also went back and checked versions of my code form before I started getting the error and now I'm getting the error there too. I'm really at a loss here... I'm new to programming and I'm pretty sure I'm doing other stuff thats bad practice, but it was working before so I dont get it! I'm including my class definitions below

Car

class ParseCar: PFObject
{
    @NSManaged var owner: PFUser?
    @NSManaged var make: String?
    @NSManaged var model: String?
    @NSManaged var year: String?
    @NSManaged var color: String?
    @NSManaged var licensePN: String?

init(owner: PFUser, make: String?, model: String?, year: String?, color: String?, licensePN: String?) {
    super.init()

    self.owner = owner
    self.make = make
    self.model = model
    self.year = year
    self.color = color
    self.licensePN = licensePN
}


extension ParseCar: PFSubclassing
{

class func parseClassName() -> String {
    return "Car"
}

override class func initialize() {
    var onceToken: dispatch_once_t = 0
    dispatch_once(&onceToken) {
        self.registerSubclass()
    }
}
}

user

class ParseUser: PFUser {

    @NSManaged var firstName: String?
    @NSManaged var lastName: String?
    @NSManaged var zipCode: String?
    @NSManaged var userRole: String?
    @NSManaged var stripeID: String?
    @NSManaged var address1: String?
    @NSManaged var address2: String?
    @NSManaged var city: String?
    @NSManaged var state: String?
    @NSManaged var zip: String?
    @NSManaged var country: String?


    override class func initialize() {
        var onceToken : dispatch_once_t = 0;
        dispatch_once(&onceToken) {
            self.registerSubclass()
    }
}

}

1 Answer 1

0

You need to implement the protocol PFSubclassing (see Parse subclassing docs).

class ParseCar: PFObject, PFSubclassing { ... }

Just a heads up, I've run into really strange bugs using the Parse SDK in Swift because I registered the subclass in the initialize method (your milage may vary).

So call ParseCar.registerSubclass() when your app launches for the first time.

Sign up to request clarification or add additional context in comments.

2 Comments

So in my initialize method for both classes I have self.registerSubclass. I should remove just that line from each class definition and in my app didFinishLaunchingWithOptions delegate method put ParseCar.registerSubclass and ParseUser.registerSubclass
If adding PFSubclassing doesn't work then try that yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.