PFQuery指针数组包含某个PFObject

时间:2015-08-25 15:11:52

标签: ios swift parse-platform

我的班级Deal中的对象有一个属性relatedContacts,它是指向Contact个对象的指针数组。在删除Contact之前,我正在运行以下查询以确定当前Deal对象是否是任何Contact中指针的目标。

let relatedContactObjects:NSArray = [self.contactObject] as NSArray

let relatedContactQuery:PFQuery = PFQuery(className: "Deal")
    relatedContactQuery.fromLocalDatastore()
    relatedContactQuery.fromPinWithName("Deals")
    relatedContactQuery.whereKey("user", equalTo: PFUser.currentUser()!)
    relatedContactQuery.whereKey("relatedContacts", containsAllObjectsInArray: relatedContactObjects as [AnyObject])

然而,这会返回Parse Error 102:“$ all所有查询都不支持值类型。”

Parse documentation表示containsAllObjectsInArray接受NSArray,但Xcode会显示警告NSArray is not implicity convertible to [AnyObject]

如何使我的查询有效?

编辑:我查看了relatedContacts的内容,似乎每个实例都包含一个字典数组,例如:[{"__type":"Pointer","className":"Contact","objectId":"BoLym053hX"},{"__type":"Pointer","className":"Contact","objectId":"AgpnxAFUBn"},{"__type":"Pointer","className":"Contact","objectId":"ob20tThdfp"}]

正如所建议的,我还查看了containedIn查询约束,但它用于标识给定数组中包含的对象。我正在尝试识别包含给定对象的数组。

2 个答案:

答案 0 :(得分:3)

Parse.com重载equalTo:允许它表示:(a)奇异属性等于操作数,或(b)数组属性包含操作数。因此,您的目标很容易说明如下:

relatedContactQuery.fromPinWithName("Deals")
relatedContactQuery.whereKey("user", equalTo: PFUser.currentUser()!)
relatedContactQuery.whereKey("relatedContacts", equalTo:self.contactObject)

答案 1 :(得分:0)

在接受答案之前,我还尝试使用循环来遍历数组,并确定它们是否包含当前对象,然后递增计数。

var dealsPointingToContactCount:Int = 0
func countDealsRelatedToContact() {
    let dealsWithRelatedContactQuery:PFQuery = PFQuery(className: "Deal")
    dealsWithRelatedContactQuery.fromLocalDatastore()
    dealsWithRelatedContactQuery.fromPinWithName("Deals")
    dealsWithRelatedContactQuery.whereKey("user", equalTo:PFUser.currentUser()!)
    dealsWithRelatedContactQuery.whereKeyExists("relatedContacts")
    dealsWithRelatedContactQuery.findObjectsInBackgroundWithBlock{(objects, error) -> Void in

        if (error == nil) {

            var dealsWithPointersToContacts:NSArray = objects! as NSArray

            for deal in dealsWithPointersToContacts {

                var dealContactsArray:NSArray = deal["relatedContacts"] as! [PFObject]

                for contact in dealContactsArray {
                    if contact as! PFObject == self.object {
                        self.dealsPointingToContactCount++
                        println("Deals pointing to current contact: \(self.dealsPointingToContactCount)")
                    }
                }
            }  
        } 
    }
}