iosparse-platformsubclassingpfuserpffile

'The class PFUser must be registered with registerSubclass before using Parse.'


I've looked around and all the other answers do not work for me, I'm trying to query PFUser, but I keep getting an exception of 'The class PFUser must be registered with registerSubclass before using Parse.'

This is where the issue occurs:

override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
    let message = messages[indexPath.item]
    if self.avatars[message.senderId] == nil {
        var imageView = JSQMessagesAvatarImage(placeholder: UIImage(named: "profile"))
        self.avatars[message.senderId] = imageView

        let user = users[message.senderId]!
        print(user)
        var parseAvatar = PFFile()

        PFUser.registerSubclass()
        let imageQuery = PFUser.query()
        imageQuery?.whereKey("objectId", equalTo: user.objectId!)
        imageQuery?.limit = 1

        imageQuery?.findObjectsInBackgroundWithBlock({ (images: [AnyObject]?, error: NSError?) -> Void in
            if imageQuery?.countObjects() == 0 {
                return
            } else {
                for image in images! {
                    if image.objectForKey("profileImage") == nil {
                        return
                    } else {
                        let userPic = image.objectForKey("profileImage") as! PFFile
                        userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                            if (error == nil) {
                                let image = UIImage(data: imageData!)
                                imageView.avatarImage = JSQMessagesAvatarImageFactory.circularAvatarImage(UIImage(data: imageData!), withDiameter: 30)
                            }
                        })
                    }

                }
            }
        })

            // Reload entire table now that the avatar is downloaded
            self.collectionView?.reloadData()

        }

    return self.avatars[message.senderId]
}

Any idea what I might be missing here? I did this in another file, and it doesn't have that issue. So I'm a little confused.

EDIT:

Like this?

import Foundation
import Parse

class TradeUser : PFUser {

override class func initialize() {
    self.registerSubclass()
}

var userID : String {
    get {return objectForKey("objectId") as! String}
    set { setObject(newValue, forKey: "objectId") }
}

var emailAddress : String {
    get {return objectForKey("email") as! String}
    set { setObject(newValue, forKey: "email") }
}

var firstName : String {
    get {return objectForKey("firstName") as! String}
    set {setObject(newValue, forKey: "firstName")}
}

var lastName : String {
    get {return objectForKey("lastName") as! String}
    set {setObject(newValue, forKey: "lastName")}
}

var primaryQueue : String {
    get {return objectForKey("primaryQueue") as! String}
    set {setObject(newValue, forKey: "primaryQueue")}
}

var image : PFFile {
    get { return self["profileImage"] as! PFFile }
    set { self["profileImage"] = newValue }
}

}


Solution

  • It seems like there's an issue with how the subclass is being registered.

    Update your PFUser subclass with the following

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