如果在iPhone应用程序运行之前打开Watch应用程序,则不会填充任何数据 [英] If Watch app opens before iPhone app runs, no data is populated

查看:107
本文介绍了如果在iPhone应用程序运行之前打开Watch应用程序,则不会填充任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,如果我在iPhone Simulator之前在Watch Simulator中运行我的应用程序,则Watch App中没有数据.

这是因为我从iPhone应用程序获取Watch数据(在其中查询Parse,并将结果保存到Watch可以访问的Group Defaults中),因为据我所知无法在Watch应用(扩展程序)中运行此类功能.

This is because I get the Watch data from the iPhone app (where I query Parse and save the results to the Group Defaults where the Watch can access it), since from what I believe we have been told there is no way to run this sort of function in the Watch app (extension).

我对此有误吗?

如果iPhone应用尚未运行,我应该如何在Watch应用首次运行时对其进行填充?

How am I supposed to populate the Watch app the first time it runs, if the iPhone app has not run yet?

iPhone TableViewController.m:

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;
    }  
}

推荐答案

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

You can query Parse from you extension and you can write to the group defaults. Your extension can make URL requests just like your iOS app. You will want to be careful to not block the main thread while the query is running. You will need to have some initial UI to show the user you are querying for initial data so that the app doesn't look frozen.

这篇关于如果在iPhone应用程序运行之前打开Watch应用程序,则不会填充任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆