当只有一个

时间:2015-05-14 00:11:35

标签: ios objective-c parse-platform

我有以下代码来查找自定义类中的对象。由于某种原因,它找到两个相同的对象(self.addedArray2' s计数为2,但在Parse数据表在线,只有一个)。你明白为什么两次找到同一个对象吗?

self.array2 = [[NSMutableArray alloc] init];
    self.addedArray2 = [[NSMutableArray alloc] init];

    PFQuery *query = [PFQuery queryWithClassName:@"Friends"];
    [query whereKey:@"user" containsString:[[PFUser currentUser] objectForKey:@"username"]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for (Friends *currentFriend in objects) {
            self.relation = currentFriend.friendsRelation;
            self.addedRelation = currentFriend.addedRelation;
            self.query = [_relation query];

            [[_relation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                [self.array2 addObjectsFromArray:objects];

                [[_addedRelation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                    [self.addedArray2 addObjectsFromArray:objects];

                    [self.segmentControl setTitle:[NSString stringWithFormat:@"Friends (%lu)", (unsigned long)[self.array2 count]] forSegmentAtIndex:0];
                    [self.segmentControl setTitle:[NSString stringWithFormat:@"Added Me (%lu)", (unsigned long)[self.addedArray2 count]] forSegmentAtIndex:1];
                    NSLog(@"Number 1: %@", [self.addedArray2 objectAtIndex:0]);
                    NSLog(@"Number 2: %@", [self.addedArray2 objectAtIndex:1]);

                }];
            }];
        }
    }];

记录" 1号"那么" 1号"那么" 2号"。看起来代码正在运行两次并再次添加它。我该如何解决这个问题?有了这些嵌套,我真的不知道我能做什么。

1 个答案:

答案 0 :(得分:2)

对于Friends类的关键“用户”,如果是包含用户用户名的String,请尝试替换:

[query whereKey:@"user" containsString:[[PFUser currentUser] objectForKey:@"username"]];

有了这个,请告诉我这是否会改变:

[query whereKey:@"user" equalTo:[[PFUser currentUser] objectForKey:@"username"]];

如果它是指向用户对象的指针,那么您将希望您的查询类似于这些行中的某些内容:

[query whereKey:@"user" equalTo:[User currentUser]];

如果我的任何一个假设都是错误的,请告诉我,因为我不知道你班级的结构,但我会试着弄明白。