Parse.com 查询以获取具有特定指针的所有项目
Parse.com Query to Get All Items with a Specific Pointer
我想从我的 Parse.com table 名为 Sticker 的特定商店获取所有商品。我的贴纸 table 有一个名为 shopId 的列。所以显而易见的解决方案是:
//get all stickers from one shop of category dress
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: "QjSbyC6k5C")
query.whereKey("category", equalTo: "DR")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
然而,这会导致此错误:
error: pointer field shopId needs a pointer value
我看到一个常见的解决方案似乎是将查询传递给实际对象而不是 ID 字符串。这是否意味着我必须首先进行单独的查询以获取特定的商店对象,然后将其传递给我的查询?或者有更短的路吗?
这是我试图获取商店的尝试,但它导致了这个错误:
Can only call -[PFObject init] on subclasses conforming to
PFSubclassing
var query1 = PFQuery(className: "Shop")
var shop1 = PFObject()
query1.getObjectInBackgroundWithId("QjSbyC6k5C") {
(shop: PFObject?, error: NSError?) -> Void in
shop1 = shop!
}
编辑:所以我的解决方案基本上按照答案的建议进行。我的代码是这样的(Glamour 是店名):
var shopQuery = PFQuery(className:"Shop")
shopQuery.getObjectInBackgroundWithId("QjSbyC6k5C") {
(glamour: PFObject?, error: NSError?) -> Void in
if error == nil && glamour != nil {
println(glamour)
//get all stickers from one shop of category dress
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: glamour!)
query.whereKey("category", equalTo: "DR")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
} else {
println(error)
}
}
我会把这个问题留在这里,也许有人会评论回答:有没有什么办法可以得到商店并给它 class 范围,这样我们就不必嵌套了第二个查询里面第一个查询成功了吗?这样会不会更优雅?
您需要传递 PFObject。使用以下内容更改您的代码
PFObject *object = ...
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: "QjSbyC6k5C")
query.whereKey("category", equalTo: object);
我想从我的 Parse.com table 名为 Sticker 的特定商店获取所有商品。我的贴纸 table 有一个名为 shopId 的列。所以显而易见的解决方案是:
//get all stickers from one shop of category dress
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: "QjSbyC6k5C")
query.whereKey("category", equalTo: "DR")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
然而,这会导致此错误:
error: pointer field shopId needs a pointer value
我看到一个常见的解决方案似乎是将查询传递给实际对象而不是 ID 字符串。这是否意味着我必须首先进行单独的查询以获取特定的商店对象,然后将其传递给我的查询?或者有更短的路吗?
这是我试图获取商店的尝试,但它导致了这个错误:
Can only call -[PFObject init] on subclasses conforming to PFSubclassing
var query1 = PFQuery(className: "Shop")
var shop1 = PFObject()
query1.getObjectInBackgroundWithId("QjSbyC6k5C") {
(shop: PFObject?, error: NSError?) -> Void in
shop1 = shop!
}
编辑:所以我的解决方案基本上按照答案的建议进行。我的代码是这样的(Glamour 是店名):
var shopQuery = PFQuery(className:"Shop")
shopQuery.getObjectInBackgroundWithId("QjSbyC6k5C") {
(glamour: PFObject?, error: NSError?) -> Void in
if error == nil && glamour != nil {
println(glamour)
//get all stickers from one shop of category dress
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: glamour!)
query.whereKey("category", equalTo: "DR")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
} else {
println(error)
}
}
我会把这个问题留在这里,也许有人会评论回答:有没有什么办法可以得到商店并给它 class 范围,这样我们就不必嵌套了第二个查询里面第一个查询成功了吗?这样会不会更优雅?
您需要传递 PFObject。使用以下内容更改您的代码
PFObject *object = ...
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: "QjSbyC6k5C")
query.whereKey("category", equalTo: object);