PFUser注册后创建PFObject和PFRelation

时间:2015-06-09 09:02:42

标签: ios swift parse-platform pfuser pfrelation

在我的结构中,我想签署一个用户并直接为其分配一个组(多对多关系模型)

以下是我如何注册用户。完成后,我不知道如何使用PFRelation与PFObject相关联。

有什么想法吗?

// SIGN UP USER
var user = PFUser();
user.email = emailTextField.text;
user.username = emailTextField.text;
user.password = passwordTextField.text;

user.signUpInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
    if error == nil {
        //Create a PFObject
        var group = CustomPFObject();
        group.name = "My First Group";
    }
});

2 个答案:

答案 0 :(得分:1)

您可以这样做:

user.signUpInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
    if error == nil {

        //Create a PFObject

        var group = CustomPFObject();
        group.name = "My First Group";

        var relation: PFRelation = group.relationForKey("your_key")

        relation.addObject(user)

        group.save() // synchronous

        group.saveInBackgroundWithBlock { (Bool, NSError?) -> Void in

        }   // async
    }
});

答案 1 :(得分:1)

如何在Parse.com中创建关系

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

如果你需要获得一个PFObject来添加到关系中,你就是这样做的:

//first you create the user that will relate with something
var user = PFUser.currentUser()
//Then you create a relationship type eg. friend, likes, score (in this case like similar to facebook or twitter
var relation = user.relationForKey("likes")
//after you add the PFObject that it relates to eg. a friend, a post, a twitte (see how to acquire this PFObejct below)
relation.addObject(post)
//Now you just need to save the relation
user.saveInBackgroundWithBlock {
  (success: Bool, error: NSError?) -> Void in
  if (success) {
    // The post has been added to the user's likes relation.
  } else {
    // There was a problem, check error.description
  }
}

我希望能帮到你!