user3140511
user3140511

Reputation: 47

Refreshing and updating objects in a NSMutableA​rray from Parse

I have an app where the user signup and every time they finish a level it is sent to Parse. Its all working great so far. Now when a user wants to add a friend, they type in their email and it adds their name to a NSMutableArray and their score to a separate NSMutableArray. Now, I need some way to update them to the newest score available. So this is what I have :

// 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];

} 

This successfully loads data when the app is closed and reopened. Now, I need it to refresh with new data and have chosen to do that every time the view appears.

So what can I do in the ViewDidAppear to update each value for the email. So I am thinking is for each email change the value in the other NSMutableArray because the indexes are equal. The problem is how can I change a value for each email in an opposite array? Would I use a for loop? Does this seem like it would work?

Upvotes: 1

Views: 189

Answers (1)

Wain
Wain

Reputation: 119031

On the query, use whereKey:matchesQuery: to find all of the score objects which are associated with friends (the sub query is to find the friends). You also want to use includeKey: so that the friend is available.

Upvotes: 1

Related Questions