后台查询完成后,如何更新表行?
我假设我需要在Parse
的awakeWithContext
查询中执行此操作?我最初将行设置为willActivate
。
如有需要,将发布任何多余的代码/信息,谢谢!
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;
}
}
最佳答案
创建一个新方法,该方法在PFQuery
回调的末尾和willActivate
方法中调用,例如:
- (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;
}
}
在这里,您可能还希望显示一个标签来告诉用户何时刷新数据。
回调的结尾看起来也应该是:
// Matchup
[defaults setObject:localMatchup forKey:@"KeyMatchup"];
NSLog(@"Default Matchup: %@", localMatchup);
self.matchupArray = localMatchup;
[self populateTable];