简体   繁体   中英

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?

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!

I solved this issue by changing the initial view controller. It didn't register the user because it didn't go through the view controller that previously stated/identified who the user was.

What I did specifically:

  1. Set the initial view controller, for the controller that captures who the user is and then segue your way past to where you want to be.
  2. Restart the simulator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM