parse.com:如何将指针键与对象进行比较?

时间:2013-09-26 08:42:13

标签: ios parse-platform

我正在使用后端服务parse.com获取iOS应用程序,我在查询时遇到问题。我需要帮助方法whereKey:matchesKey:inQuery;

我有这段代码:

//NOT WORKING
PFQuery *query1 = [PFQuery queryWithClassName:@"Object"];

PFQuery *query2 = [PFQuery queryWithClassName:@"ObjectsRelations"];
[query2 whereKey:@"user" equalTo:[PFUser currentUser]];

[query1 whereKey:@"objectId" matchesKey:@"objectPointer" inQuery:query2];
[query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    // No objects
}];

它不按我想要的方式工作。我已经尝试了几种方法来比较类“ObjectsRelations”中的键“objectPointer”(这是一个指向Object类的实例的指针)和查询1中的实际Object。我没有得到任何对象,因为比较因为关键的objectId只是一个字符串,而关键的objectPointer是一个指向Object的指针,所以它不能正常工作。

当我运行此代码时,我得到了预期的结果,但这需要我做两个api请求才能将实际的objectId作为字符串!

//WORKING
PFQuery *query = [PFQuery queryWithClassName:@"Object"];

PFQuery *query2 = [PFQuery queryWithClassName:@"ObjectRelations"];
[query2 whereKey:@"user" equalTo:[PFUser currentUser]];

[query2 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    PFObject *firstObject = [((PFObject*)[objects firstObject]) objectForKey:@"objectPointer"];

[query whereKey:@"objectId" equalTo:firstObject.objectId];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    //Getting the objects correctly from the class Object!
}];
}];

如何使用单个api请求执行此操作?如何将类的实例与带有查询的类的指针进行比较?

这是我想要做的事情:(伪代码)

[query1 where:SELF matches:@"objectPointer" inQuery:query2];

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

我刚刚搜索了这个完全相同的问题,Parse论坛上有很多关于它的问题:

Trouble with nested query using object_id
Assistance with relational query
Compare string to pointer in query with does not match key in query

第一个解释了在ObjectRelations课程中包含额外字段(在本例中)的黑客攻击。该键/字段将是一个字符串,它将是指针的objectId。它将是保存指针的键/字段的补充。

如果你仔细阅读这些问题,你可以看到,截至目前,Parse似乎没有直接回答这个问题。

答案 1 :(得分:1)

我建议在每个对象上添加一个类型为指针的列,而不是添加类型为 String 的附加列,以指向对象本身。这将大大减少您必须添加的列数量。唯一的缺点是必须在Cloud Code上完成。

this question的情况下,您将有两个查询:

//Inner query
//Library containing pointer<Deck> & pointer<User>
PFQuery * subscriptions = [PFQuery queryWithClassName:@"subscription"];
[subscriptions whereKey:@"User" equalTo:[PFUser currentUser]];
//Outer query
//Pull down a list of deckStore objects not included in the subscriptions for current user
PFQuery * decks = [PFQuery queryWithClassName:@"deckStore"];

然后代替:

[decks whereKey:@"objectId" doesNotMatchKey:@"deckString" inQuery:subscriptions];

你可以这样做:

[decks whereKey:@"this" doesNotMatchKey:@"deck" inQuery:subscriptions];

以下是应添加Cloud Code的示例:

Parse.Cloud.afterSave("Deck", function(request) {
    var deck = request.object;

    // To make sure this is the first time of "afterSave" of this object.
    if (deck.createdAt.getTime() == deck.updatedAt.getTime()) {
        // "this" is the column which contains the pointer of the object itself.
        if (deck.get("this") == null) {
            deck.set("this", deck);
            deck.save();
        }
    }
}

答案 2 :(得分:0)

我有同样的问题。这是我的解决方案:

PFQuery *query1 = [PFQuery queryWithClassName:@"tableClass"];
[query1 whereKey:@"objectId" equalTo:[(PFObject *)[object objectForKey:@"pointerField"] objectId]];

PFObject *obj1 = [query1 getObjectWithId:[(PFObject *)[object objectForKey:@"pointerField"] objectId]];
NSString *pointerName = [obj1 objectForKey:@"name"];