Search code examples
iosswiftparse-platformpfuser

Parse PFUser Subclass Sign Up using Swift Best Practice


I have just downloaded and experimented with Parse.

Everything works as expected, but I have a question regarding a good subclassing design.

The standard PFUser has THREE fields by default.

  1. username
  2. emailAddress
  3. password

I would like to add a few more "column" to the User table. Those are:

  1. firstName
  2. lastName
  3. phone (Optional)
  4. profilePicture

Below is my subclass design, but I am not sure if this is the way one might want to subclass their PFUser.

import UIKit

class GPUser: PFUser, PFSubclassing {
    var firstName: String? {
        didSet {
            self["firstName"] = firstName;
        }
    }
    var lastName: String? {
        didSet {
            self["lastName"] = lastName;
        }
    }
    var phone: String? {
        didSet {
            self["phone"] = phone;
        }
    }
    var profilePicture: PFFile? {
        didSet {
            self["profilePicture"] = profilePicture;
        }
    }
}

Questions:

  1. I am wondering what is the best approach to subclass a PFUser (in order to add columns to it) and of course the best design possible. Do you have any design in mind?

  2. should we override the init method and instantiate all of the variables (firstName, lastName, phone, etc) so that the optional fields column get created on signUp?

  3. Should I get rid of those didSets blocks? If yes, could you suggest on a better design?


Solution

  • In my opinion, the easiest way to achieve what you want is to use stored @NSManaged properties:

    class GPUser: PFUser, PFSubclassing {
        @NSManaged var firstName: String
        @NSManaged var lastName: String
        @NSManaged var phone: String
        @NSManaged var profilePicture: PFFile
    
        class func parseClassName() -> String! {
            return "YourParseClassName"
        }
    }
    

    Don't forget to register for your custom PFUser class in the AppDelegate:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        GPUser.registerSubclass()
    }