我已将数据保存在Parse中存储组名的类中。每个组名称还具有包含所有组信息的关系。我试图查询关系中的数据,但输出没有产生任何结果。
第一个图像是包含组名称和组信息关系列的类(“组”),第二个图像是包含所有组数据的“GroupInfo”关系。
对象的保存方式如下:
let groups = PFObject(className: "Groups")
let groupinfo = PFObject(className: "Groupinfo")
@IBAction func CreateNewGroupAction(sender: AnyObject) {
groupinfo["Score"] = 1
groupinfo["Project"] = "Test"
beaterinfo.save()
groups["GroupName"] = "DFG"
let relation = groups.relationForKey("Groupinfo")
relation.addObject(groupinfo)
beatergroups.save()
}
这是我尝试查询关系数据:
@IBAction func QueryDataAction(sender: AnyObject) {
var groupnamesquery = PFQuery(className: "Groups")
groupnamesquery.whereKey("GroupInfo", equalTo: "DFG")
beatergroupnamesquery.findObjectsInBackgroundWithBlock { (object: [AnyObject]?, error: NSError?) -> Void in
if error == nil && object != nil {
if let object = object as? [PFObject] {
for objects in object {
let relation = objects.relationForKey("GroupInfo")
println(relation.valueForKey("Project")!)
}
}
} else {
println("Error: \(error)")
}
}
}
非常感谢任何有关查询关系数据的帮助!
答案 0 :(得分:0)
“QueryDataAction”第二行出错:
groupnamesquery.whereKey("GroupInfo", equalTo: "DFG")
应该是
groupnamesquery.whereKey("**GroupName**", equalTo: "DFG")
。
答案 1 :(得分:0)
第一个查询仅返回不包含任何“GroupInfo”信息的“Groups”对象。因此,如果您执行嵌套查询,则应该能够检索GroupInfo对象。
@IBAction func QueryDataAction(sender: AnyObject) {
var groupnamesquery = PFQuery(className: "Groups")
groupnamesquery.whereKey("GroupName", equalTo: "DFG")
beatergroupnamesquery.findObjectsInBackgroundWithBlock({
(object: [AnyObject]?, error: NSError?) -> Void in
if error == nil && object != nil {
if let object = object as? [PFObject] {
for objects in object {
// create a relation based on the groupsinfo key
let relation = objects.relationForKey("GroupInfo")
// generate a query based on that relation
let query = relation.query()
// execute the query
let groupInfo = query.findObjects()
println(groupInfo["Project"])
}
}
} else {
println("Error: \(error)")
}
})
}
答案 2 :(得分:0)
想出来:
@IBAction func QueryDataAction(sender: AnyObject) {
var groupnamesquery = PFQuery(className: "Groups")
groupnamesquery.whereKey("GroupName", equalTo: "DFG")
beatergroupnamesquery.findObjectsInBackgroundWithBlock({
(object: [AnyObject]?, error: NSError?) -> Void in
if error == nil && object != nil {
if let object = object as? [PFObject] {
for objects in object {
let relation = objects.relationForKey("GroupInfo")
let query = relation.query()
query?.findObjectsInBackgroundWithBlock({ (objecx: [AnyObject]?, erro: NSError?) -> Void in
if error == nil && objecx != nil {
if let objecx = objecx as? [PFObject] {
for obecxs in objecx {
let infos: AnyObject? = obecxs.valueForKey("Project")!
println(infos!)
}
}