2

我正在开发一个 AppleWatch 应用程序,我想知道是否可以在后台更新一瞥。当用户打开 Glance 时,我希望立即更新 Glance,而不是等到 Glance 从服务接收到更新的信息并呈现它。

目前我正在使用此功能更新一瞥,但这将显示更新的信息不是即时的。

- (void)awakeWithContext:(id)context // This function can't spend too much time processing the data otherwise we will se a black background. Can't call the service here.
{
    //Configure the interface using the cached info. 
    [super awakeWithContext:context];
}


- (void)willActivate 
{
    //Configure the interface using the service
    [super willActivate];
}

有任何想法吗?

提前致谢。

4

2 回答 2

1

您可以在您的 iOS 应用程序中安排后台获取,然后在获取之后,将数据保存到共享组容器中。然后,在您的 Glance 控制器的 awakeWithContext 方法中,您可以从该共享组容器中获取数据。它应该足够快。

于 2015-01-15T16:14:25.730 回答
-1

第一次出现您的一瞥时,您可以在init. 然后,在willActivate(或者,我想,awakeWithContext:)填充您的控件并启动一个计时器以定期获取更新的内容以供查看。

正如 Ivp 所建议的,您应该将更新的数据存储在共享容器中。(看看NSUserDefaults:initWithSuiteName:

如果您知道您的 iPhone 应用程序将在您的 Glance 启动之前启动,您可以跳过一般的欢迎消息,而只依靠它来为共享容器中的数据播种。

这是我上面描述的示例,尽管它没有显示数据在共享容器中的存储。

- (instancetype)init {
    self = [super init];

    if (self) {
        if (isFirstLaunch) {
            self.displayInfo = [self createWelcomeInfo];
        }
    }

    return self;
}

- (void)willActivate {
    [super willActivate];

    // refreshUI will check for new data, request more for next time,
    // and then configure the display labels with the new data.
    [self refreshUI]; 

    // Setup a timer to refresh the glance based on new trends provided by the phone app.
    // NOTE: This timer is never invalidated because didDeactivate seems to never be called.
    //       If, by chance, willActivate is called more than once, we may end up with multiple repeating timers.
    self.refreshTimer = [NSTimer scheduledTimerWithTimeInterval:kTimeIntervalRefresh
                                                         target:self
                                                       selector:@selector(refreshUI)
                                                       userInfo:nil
                                                        repeats:YES];
}

- (void)refreshUI {
    if (! isFirstLaunch ) {
        if (self.nextDisplayInfo) {
            self.displayInfo = self.nextDisplayInfo;
        }

        // requestMoreInfo will populate self.nextDisplayInfo asynchronously,
        // so it's ready for the next time you are in refreshUI
        [self requestMoreInfo];
    }

    [self configureLabels:self.displayInfo];

    // Setup for when the user taps the glance
    [self updateUserActivity:@"com.example.AppName" userInfo:@{@"data": self.displayInfo} webpageURL:nil];    
}
于 2015-04-07T17:00:39.430 回答