PFQuery FindObjectsInBackground返回0

时间:2017-09-12 03:53:55

标签: ios xcode parse-platform pfquery pfobject

在我的UIViewController我试图查询我的解析服务器,但我一直得到0的回报,虽然我知道100%这个类确实有对象。有什么想法吗?

 PFQuery *query = [PFQuery queryWithClassName:@"General"];

 int i;
 for (i = 0; i < [follows count]; i++) {
        [query whereKey:@"Session" containedIn:follows];
 }
 query.cachePolicy = kPFCachePolicyCacheThenNetwork;

 [query orderByDescending:@"createdAt"];
 [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
 // it never gets here...
 NSLog(@"OBJECTS%@", objects);
 if (!error) {
     NSLog(@"Successfully retrieved %lu objects.", (unsigned long)objects.count);
     for (PFObject *object in objects) {
         NSLog(@"%@", object.objectId);
     }

     // [self gotoMain];
 } else {
       NSLog(@"Error: %@ %@", error, [error userInfo]);
   }
 }];

它告诉我在控制台中成功检索0个对象没有错误。

1 个答案:

答案 0 :(得分:1)

正如其他已经建议的那样,我会先做最简单的查询:

PFQuery *query = [PFQuery queryWithClassName:@"General"];
 [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
 if (!error) {
     NSLog(@"Successfully retrieved %lu objects.", (unsigned long)objects.count);
 } else {
       NSLog(@"Error: %@ %@", error, [error userInfo]);
   }
 }];  

如果执行没有错误,则返回0个对象,仪表板显示有应返回的对象,类名必须是错误的。所以请仔细检查班级名称,例如拼写。

如果返回了对象,则过滤器必定是错误的。您for出于以下两个原因是错误的:
1)for循环执行follows.count - 次,但它始终执行相同的指令,因为未使用index。我想你想写(但这也错了

 for (i = 0; i < [follows count]; i++) {
        [query whereKey:@"Session" containedIn:follows[i]];
 }  

2)这是错误的,因为你只能有一个过滤器whereKey:containedIn:。正如DevKyle所提到的,这个单个过滤器会被覆盖follows.count-1次 - 并且只使用最后一个过滤器。
我想你想要有类似于各个过滤器的逻辑OR。如果是这样,您必须展平您的数组,即在NSArray *flattenedFollows中创建所有元素的单个数组follows[i],请参阅ILoggingBuilder.ClearProviders然后设置一个过滤器

 [query whereKey:@"Session" containedIn: flattenedFollows];  

修改
最后一个想法:如果你的查询是正确的(除了for循环)并且无论如何都没有返回任何对象,那么你可能没有权利访问它们。因此,请检查这些记录的ACL字段是否具有正确的访问权限。