Stackmob SMQuery OR条件:“排序不能与OR语句堆栈使用”

时间:2014-01-06 06:39:01

标签: stackmob

我正在使用Stackmob SMQuery并尝试使用4 OR条件进行查询,并对所有结果进行排序。我的代码如下。问题是

        SMQuery *query = [[SMQuery alloc] initWithSchema:STACKMOB_SCHEMA_TESTIMONIAL];

        [query where:@"friend_id" isIn:[friendFacebookIds arrayByAddingObject:[KKKeychain getStringForKey:@"facebookId"]]];

        // WHERE (privacy == Public OR (privacy == Private AND friend_id == myFacebookId) OR (privacy == Restricted AND myFacebookId NOT IN restricted_Ids))

        SMQuery *subQuery0 = [[SMQuery alloc] initWithSchema:STACKMOB_SCHEMA_TESTIMONIAL];
        SMQuery *subQuery1 = [[SMQuery alloc] initWithSchema:STACKMOB_SCHEMA_TESTIMONIAL];
        SMQuery *subQuery2 = [[SMQuery alloc] initWithSchema:STACKMOB_SCHEMA_TESTIMONIAL];
        SMQuery *subQuery3 = [[SMQuery alloc] initWithSchema:STACKMOB_SCHEMA_TESTIMONIAL];

        [subQuery0 where:@"privacy" isEqualTo:nil]; // just in case no privacy was set, assume it is public
        [subQuery0 orderByField:@"createddate" ascending:NO];

        [subQuery1 where:@"privacy" isEqualTo:[NSNumber numberWithInt:TTPrivacyLevelPublic]];
        [subQuery1 orderByField:@"createddate" ascending:NO];

        [subQuery2 where:@"privacy" isEqualTo:[NSNumber numberWithInt:TTPrivacyLevelPrivate]];
        [subQuery2 where:@"user_id" isEqualTo:[KKKeychain getStringForKey:@"facebookId"]]; // for private post, I can only see posts that were created by me
        [subQuery2 orderByField:@"createddate" ascending:NO];

        [subQuery3 where:@"privacy" isEqualTo:[NSNumber numberWithInt:TTPrivacyLevelRestricted]];
        [subQuery3 where:@"restricted_user_ids" isNotEqualTo:[KKKeychain getStringForKey:@"facebookId"]]; // make sure I'm not restricted to see
        [subQuery3 orderByField:@"createddate" ascending:NO];

        // combine subquery to main query

        [query and:[[[subQuery1 or:subQuery2] or:subQuery3] or:subQuery0]];

        [query orderByField:@"createddate" ascending:NO];
        ....

所以如果我评论了最后一行“[query orderByField:@”createddate“ascending:NO];”然后它工作正常。但结果将是4个不同数据集的组合,这些数据集未按创建顺序排序。我也尝试在每个子查询中进行排序,但结果也不正确,因为它们只在每个子查询中排序,而不是与其他结果组合。

感谢。

1 个答案:

答案 0 :(得分:0)

数据库不支持执行包含OR和排序的查询 - 这是数据库限制。相反,在StackMob上执行查询,然后使用sortedArrayUsingComparator或类似的东西通过createddate对结果进行本地排序。