无法将PFRelation值添加到特定对象

时间:2015-07-22 18:02:33

标签: ios swift parse-platform

我正在创建一个应用程序,用户可以在其中创建一个组,然后在该组中添加一些信息。为此,在解析数据库中创建PFRObject,并为包含所有组信息的对象创建PFRelation。我无法将关系信息添加到我想要的对象中。它不是将应用程序添加到现有对象的应用程序,而是创建新对象和关系,并将信息添加到其中。如何将数据添加到现有对象?非常感谢任何建议。

@IBAction func CreateNewGroupAction(sender: AnyObject) {
    var groupinfo = PFObject(className: "GroupInfo")
    groupinfo["User"] = PFUser.currentUser()?.username
    groupinfo["Interest"] = "Cars"
    groupinfo.save()

    let relationinfo = PFObject(className: "BeaterGroups")
    relationinfo["GroupName"] = "CarGuys"
    let relation = relationinfo.relationForKey("GroupInfo")
    relation.addObject(groupinfo)

    relationinfo.save()

}




 var dataparse: NSMutableArray = NSMutableArray()

func loaddata (){


    var findgroups: PFQuery = PFQuery(className: "BeaterGroups")
    findgroups.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil && objects != nil {

            if let objects = objects as? [PFObject] {

                for object in objects {

                    self.dataparse.addObject(object)

                }

            }

        }

  self.tableView.reloadData()
    }


}



override func viewDidLoad() {
    super.viewDidLoad()
    loaddata()



}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataparse.count
}





override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {



    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellidentifier, forIndexPath: indexPath) as! UITableViewCell
    let cellDataParse: PFObject = self.dataparse.objectAtIndex(indexPath.row) as! PFObject
    cell.textLabel?.text = cellDataParse.objectForKey("GroupName")! as? String
 return cell
    }
}

1 个答案:

答案 0 :(得分:0)

是的,使用您当前的代码可能会出现此行为。 let relationinfo = PFObject(className: "BeaterGroups")正在创建一个新对象,因此会添加一个新行。

您需要做的是将关系添加到现有的BeaterGroups对象。您可以使用查询来获取此信息,例如