swiftparse-platformpfuser

Getting username of Parse user


I have code that i am trying to get a username of a PFUser. I'm getting the user and able to print it out, but when I get to the line for the username, the code just stops. No crash nothing, just stops running?

Any ideas why it would do that?

    print(employeeObject)
    firstName.text = employeeObject.firstName
    lastName.text = employeeObject.lastName
    if employeeObject.roleType != nil {
        roleLabel.text = employeeObject.roleType?.roleName
    }
    if employeeObject.objectForKey("userPointer") != nil {
        employeeObject.userPoint = employeeObject.objectForKey("userPointer") as! PFUser
    }
    if employeeObject.userPoint != nil {
        let userID = employeeObject.userPoint!.objectId!
        let query = PFUser.query()
        query?.whereKey("objectId", equalTo: userID)
        query?.getFirstObjectInBackgroundWithBlock({ (userData : PFObject?, error : NSError?) in
            print(userData)
            self.userLoginSwitch.setOn(true, animated: false)
            self.userNameTextField.text = self.employeeObject.userPoint?.username!
            self.passwordTextField.text = ""
            self.emailAddressTextField.text = self.employeeObject.userPoint!.email
            if self.employeeObject.userPoint!.objectForKey("isAdmin") as! Bool {
                self.adminSwitch.setOn(true, animated: false)
            }
        })

    }
    if employeeObject.active {
        disableEnableEmployee.setTitle("Disable Employee", forState: .Normal)
    } else {
        disableEnableEmployee.setTitle("Enable Employee", forState: .Normal)
        disableEnableEmployee.setTitleColor(UIColor.blueColor(), forState: .Normal)
    }

I have userPoint casted as a PFUser, and then in Parse i have it pointed to the User table, and when i print. i can see all the information. It just stops working thought, with no error or explanation.

var userPoint : PFUser? {
    get {return objectForKey("userPointer") as? PFUser}
    set { setObject(newValue!, forKey: "userPointer") }
}

Solution

  • When you fetching objects from Parse pointer objects are not included by default. There is only PFObject with objectId. If you want to include all of properties of pointer you need to use includeKey method with name of pointer. So if you get employeeObject from query (somewhere earlier) you should add line:

    employeeQuery.includeKey("userPointer")
    

    In this solution you wont need to get this user again because its the same object.

    @Mazels answer is the second solution. You getting the same user by objectId so you can read data from usedData

    self.userNameTextField.text = userData["username"]
    self.emailAddressTextField.text = userData["email"]
    if userData["isAdmin"] as! Bool { ...
    

    Last thing:

    if employeeObject.objectForKey("userPointer") != nil {
        employeeObject.userPoint = employeeObject.objectForKey("userPointer") as! PFUser
    }
    

    This is redundant. Look at your implementation of userPoint and this if statement. It really do nothing :)