2

AppleWatch アプリを開発していますが、バックグラウンドでグランスを更新できるかどうかを知りたいです。ユーザーがグランスを開いたときに、グランスを即座に更新し、グランスがサービスから更新された情報を受信して​​表示するまで待ちません。

現在、この関数を使用してグランスを更新していますが、これはすぐに更新された情報を表示しません。

- (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 アプリが起動されることがわかっている場合は、一般的なウェルカム メッセージをスキップして、共有コンテナーにデータをシードするだけに頼ることができます。

上記で説明した内容のサンプルを次に示しますが、共有コンテナー内のデータのストレージは示していません。

- (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 に答える