如果Watch应用程序在iPhone应用程序运行之前打开,则不会填充数据

时间:2015-03-11 22:52:54

标签: ios objective-c iphone parse-platform watchkit

我遇到了一个问题,如果我在iPhone模拟器之前在Watch Simulator中运行我的应用程序,那么Watch应用程序中就没有数据。

这是因为我从iPhone应用程序(我在其中查询Parse并将结果保存到Watch可以访问的组默认值中获取Watch数据),因为我相信我们拥有被告知无法在Watch应用程序(扩展程序)中运行此类功能。

我对此不正确吗?

如果iPhone应用尚未运行,我应该在第一次运行时如何填充Watch应用程序?

iPhone TableViewController.m

- (void)viewDidLoad {
    PFQuery *query = [self queryForTable];
    // ... Query Parse to find data, then save it App Group...
    NSString *container = @"group.com.me.off";
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
    // ... finish saving to App Group
}

观看`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;
    }  
}

1 个答案:

答案 0 :(得分:1)

您可以从扩展程序中查询Parse,您可以写入组默认值。您的扩展程序可以像您的iOS应用程序一样发出URL请求。在查询运行时,您需要小心不要阻塞主线程。您需要有一些初始UI来向用户显示您正在查询初始数据,以便应用程序看起来不会被冻结。