Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing Parse's PFUser in Swift

First off, I know similar questions have been asked before and I've tried following the advice of this stackoverflow answer here to no avail. I also tried adding the basic gist of this as a comment, but I don't have enough rep yet :( Basically I am trying to use PFSubclassing to extend Parse's PFUser model. As such, here's my corresponding code:

User.swift:

import Foundation
import CoreLocation

class User : PFUser, PFSubclassing {

    override init() {
        super.init()
    }

    convenience init(email: String!) {
        self.init()

        self.email = email
        self.username = email

    }

    // don't need to call User.registerSubclass() in AppDelegate because this
    // is handling that here
    override class func load() {
        self.registerSubclass()
    }

    // Commented out because this is extending PFUser
    //    override class func parseClassName() -> String! {
    //        return "PFUser"
    //    }
}

Result on Tests:

-[PFObject _loadSensitiveUserDataFromKeychainItemWithName:]: unrecognized selector sent to instance 0x7f98ebc1c250

I'm also doing the following in my Bridging-Header:

#import <Parse/Parse.h>
#import <Parse/PFObject+Subclass.h>

If I un-comment "parseClassname() in User.swift" I get:

failed: caught "NSInvalidArgumentException", "Cannot initialize a PFUser with a custom class name."

Which leads me to believe that setting up the interfaces is at least partially working.

Based on all of the advice I've seen, I can't seem to figure out what I'm doing wrong. There is also an open bug that produces the same error message, but it is caused by using Parse's local data store and I haven't configured any of that.

At this point I feel like I am missing something dead obvious, or I am being affected by the same bug as mentioned above. Any suggestions?

like image 634
Derek Dowling Avatar asked Sep 28 '22 18:09

Derek Dowling


1 Answers

Okay, solved my own problem here through trial and error. Essentially it looks like a cached logged in PFUser was causing problems. When Parse would initially load it was loading the logged in state from the emulator which was conflicting with the newly registered User subclass.

To fix this you can either:

  1. Restart the emulator.
  2. Call PFUser.logOut() right after Parse.setApplicationId() in AppDelegate to flush the cached user.

Hopefully this helps someone else out in the future!

like image 182
Derek Dowling Avatar answered Oct 05 '22 07:10

Derek Dowling