从Parse刷新和更新NSMutableArray中的对象

时间:2014-01-04 19:36:40

标签: ios iphone nsmutablearray parse-platform

我有一个用户注册的应用程序,每次他们完成一个级别,它就被发送到Parse。到目前为止它一切都很好用。现在,当用户想要添加朋友时,他们会输入他们的电子邮件,并将其名称添加到NSMutableArray,并将他们的分数添加到单独的NSMutableArray。现在,我需要一些方法将它们更新为可用的最新分数。所以这就是我所拥有的:

// This is called when an add button is pressed

- (void)launchingAlertForEmail {

    UIAlertView *emailAlertView = [[UIAlertView alloc] initWithTitle:@"Add a friend" message:@"Type in your friends email" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add friend", nil];

    emailAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;

    [emailAlertView show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 1) {

        friendEmail = [[alertView textFieldAtIndex:0] text];

        [self findUserMatchingEmail];

    }

}

- (void)findUserMatchingEmail {

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

    [query whereKey:@"username" equalTo:friendEmail];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (!error) {

            // The find succeeded.

            NSLog(@"Successfully retrieved %d scores.", objects.count);

            // Do something with the found objects

            for (PFObject *object in objects) {

                NSLog(@"%@", object.objectId);

            }

            if (objects.count == 0) {

                NSLog(@"Tis true");

            } else {

                [self insertNewObject:self];

            }

        } else {

            // Log details of the failure

            NSLog(@"Error: %@ %@", error, [error userInfo]);

        }

    }];

}

- (void)insertNewObject:(id)sender {

    if ([_objects containsObject:friendEmail]) {

        NSLog(@"yea");

    }    

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (!_objects) {

        _objects = [defaults mutableArrayValueForKey:@"_objects"];

    }

    [_objects insertObject:friendEmail atIndex:0];

    if (!objects2) {

        objects2 = [defaults mutableArrayValueForKey:@"objects2"];

    }

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

    [query whereKey:@"username" equalTo:friendEmail];

    NSArray *usersPosts = [query findObjects];

    for (NSDictionary *friendData in usersPosts) {

        NSString *intervalString = [friendData objectForKey:@"interval"];

        float interval = [intervalString floatValue];

        NSString *newInterval = [NSString stringWithFormat:@"Interval : %.0f minutes", interval / 60];

        [objects2 insertObject:newInterval atIndex:0];

    }

    if ([_objects containsObject:friendEmail]) {

        NSLog(@"yea");

    }

    [defaults setObject:_objects forKey:@"_objects"];

    [defaults setObject:objects2 forKey:@"objects2"];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

} 

当应用关闭并重新打开时,这会成功加载数据。现在,我需要用新数据刷新它,并且每次出现视图时都选择这样做。

那么我可以在ViewDidAppear中做些什么来更新电子邮件的每个值。所以我想每个电子邮件都会更改其他NSMutableArray中的值,因为索引是相等的。问题是我如何更改相反数组中每封电子邮件的值?我会使用for循环吗?这看起来有用吗?

1 个答案:

答案 0 :(得分:1)

在查询中,使用whereKey:matchesQuery:查找与朋友关联的所有分数对象(子查询是查找朋友)。您还想使用includeKey:以便朋友可以使用。