1

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?

2
  • So you're trying to refresh the scores of all friends? Give some details on the the parse data model. Commented Jan 4, 2014 at 20:01
  • Yes, all the friends in the array. What I have in Parse is the user and there interval. Commented Jan 4, 2014 at 20:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I don't think you quite understand my question. I am able to get friends scores, the problem is updating them every time the view appears
Not clear on the issue. What is the problem with just sending the query again and updating the UI in the completion block?
How do I replace the value for the username? I dont want any duplicates
It's very hard to make sense of your data structure. You should have a dictionary mapping the friends to scores (and sort this to get your data to display).
I have a row for each . The username value is their email and then there is a score value that is being updated constantly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.