Search code examples
iosswiftparse-platformpfuser

Save "followers" list in _User class using Parse


i am trying this, whenever i tap on follow button it runs this code in the table view .

@IBAction func followButtonTapped(sender: UIButton){



    sender.setTitle("unfollow", forState: UIControlState.Normal)

    var followers:PFQuery = PFQuery(className: "_User")
    followers.getObjectInBackgroundWithId(userIds[sender.tag], block: { (objects:PFObject?, error:NSError?) -> Void in

        if error != nil {


        } else if let object = objects {

            object.addUniqueObject(PFUser.currentUser()!.objectId!, forKey: "followers")

            object.saveInBackgroundWithBlock({ (success, error) -> Void in

                if error != nil {


                } else {

                    println("saved")
                }


            })


        }




    })



}

i m getting this error :

Error]: Caught "NSInternalInconsistencyException" with reason "User cannot be saved unless they have been authenticated via logIn or signUp"

how far i know is that this is because we can't update the _User class until the user is being updated is the current user.. So i want to save all the followers of a user in _User class.. Is it possible somehow??


Solution

  • Are you sure that the column followers is an Array so you can add values to it? By the way, as you are already checking first if it has an error, print it if you found.

    println("Found an error: \(error!.description)")
    

    EDIT:

    Well, now I got what you're planning to do. Add the current user to the follower column of your other user.

    1. You can either save the ACL public to Write and the continue this but this isn't really secure.
    2. Or you can create another class "Follow" where you specify who is following "fromUser column" and who is followed "toUser column", both pointers to _User, this way you can even track following relation when you want to do an unfollow. This way makes your work easier.

    To get all followers from someone:

    let followers = PFQuery(class: "Follow")
    followers.whereKey("toUser", equalTo: PFUser.currentUser()!)
    

    to create, delete e edit these relations, use PFQuery with the correct whereKeys. Tip: use

    followers.includeKey("_User")
    

    so you can fetch the pointers and save server requests if you are willing to use the followers data, like names to present in a table view.