Search code examples
iosobjective-cparse-platformwatchkit

Update Table Rows in WatchKit


How would I update my table Rows once the Background query is finished?

I'm assuming I need to do it in my Parse query in my awakeWithContext? I initially set the rows in willActivate.

Will post any extra code/info if needed just let me know, thanks!

WatchKit InterfaceController.m:

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    // Initialize Parse.
    [Parse setApplicationId:@"xxx"
                  clientKey:@"xxx"];

    // GMT Date from Phone
    NSDate *gmtNow = [NSDate date];
    NSLog(@"GMT Now: %@", gmtNow);

    // Query Parse
    PFQuery *query = [PFQuery queryWithClassName:@"na"];

    [query whereKey:@"dateGame" greaterThanOrEqualTo:gmtNow];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            NSMutableArray *localMatchup = [@[] mutableCopy];

            for (PFObject *object in objects) {
                // Add objects to local Arrays
                [localMatchup addObject:[object objectForKey:@"matchup"]];

                // App Group
                NSString *container = @"group.com.me.off";
                NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];

                // Matchup
                [defaults setObject:localMatchup forKey:@"KeyMatchup"];
                NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
                NSLog(@"Default Matchup: %@", savedMatchup);
                savedMatchup = self.matchupArray;
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Say you went to dispatch");
            });   
        }
    }];

    // App Group
    NSString *container = @"group.com.me.off";
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];

    // Matchup
    NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
    self.matchupArray = savedMatchup;
}

- (void)willActivate {
    [super willActivate];

    // Hide "No Games" Label
    if ([self.matchupArray count] > 0) {

        self.noGamesLabel.hidden = YES;
        self.rowTable.hidden = NO;

        [self.rowTable setNumberOfRows:[self.matchupArray count] withRowType:@"rows"];

        for (NSInteger index = 0; index < [self.timeArray count]; index++) {
            // Matchup
            NSString *matchupString = [self.matchupArray objectAtIndex:index];
            RowController *controller = [self.rowTable rowControllerAtIndex:index];
            [controller.matchupLabel setText:matchupString];
        }
    }
    // Show "No Games" Label
    else {
        self.rowTable.hidden = YES;
        self.noGamesLabel.hidden = NO;
    }  
}

Solution

  • Create a new method that is called at the end of your PFQuery's callback and in the willActivate method, such as:

    - (void)populateTable {
        // Hide "No Games" Label
        if ([self.matchupArray count] > 0) {
    
            self.noGamesLabel.hidden = YES;
            self.rowTable.hidden = NO;
    
            [self.rowTable setNumberOfRows:[self.matchupArray count] withRowType:@"rows"];
    
            for (NSInteger index = 0; index < [self.matchupArray count]; index++) {
                // Matchup
                NSString *matchupString = [self.matchupArray objectAtIndex:index];
                RowController *controller = [self.rowTable rowControllerAtIndex:index];
                [controller.matchupLabel setText:matchupString];
            }
        }
        // Show "No Games" Label
        else {
            self.rowTable.hidden = YES;
            self.noGamesLabel.hidden = NO;
        }
    }
    

    In here you may wish to also show a label to tell the user when the data is being refreshed.

    The end of the callback also looks like it should be:

    // Matchup
    [defaults setObject:localMatchup forKey:@"KeyMatchup"];
    NSLog(@"Default Matchup: %@", localMatchup);
    self.matchupArray = localMatchup;
    [self populateTable];