1

同期プロセスに 20 秒かかる場合があります (めったにありません)。バックグラウンドでセレクターを呼び出していました。同期が完了したという通知でアニメーションを停止する進行状況インジケーターがあり、以下のコードではすぐに変更されます。

[self performSelectorInBackground:@selector(updateFunction) withObject:nil];

しかし、進行を継続させるために、バックグラウンド タスクを使用することにしました。

    UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance

    __block UIBackgroundTaskIdentifier background_task; //Create a task object

    background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
        [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
        background_task = UIBackgroundTaskInvalid; //Set the task to be invalid

        //System will be shutting down the app at any point in time now
    }];

    //Background tasks require you to use asyncrous tasks

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Perform your tasks that your application requires

        NSLog(@"\n\nRunning in the background!\n\n");
        [self updateFunction];

        [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
        background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
        NSLog(@"Finished in the background!");
    });

しかし今では、タスクが終了すると通知が送信 (および実行) されますが、テーブル内のセルは (場合によっては) かなりの時間が経過するまで更新されません。そのため、バックグラウンド タスクなどからの「ニーズ表示」はトリガーされず、定期的な更新間隔などでのみ更新されるように思えます。

バックグラウンド タスクを使用してテーブル セルを更新する方法はありますか?

編集:通知リスナーは次のとおりです。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(refreshBttn)
                                             name:@"finishedUpdate"
                                           object:nil]; 

コードは次のとおりです。

- (void) refreshBttn {
    NSLog(@"Refresh Buttun action");
    iDomsAppDelegate *delegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];
    if([delegate updating]){
        [_updateBttn setHidden:TRUE];
        [_activityIndicator startAnimating];         
    } else {
        [_updateBttn setHidden:FALSE];
        [_activityIndicator stopAnimating];        
    }
    [self setNeedsDisplay];
    [self setNeedsLayout];
}
4

3 に答える 3

2

必要なキューから通知を確実に受け取ることができます...

[[NSNotificationCenter defaultCenter] addObserverForName:SomeNotification
                                                  object:sender
                                                   queue:operationQueue
                                              usingBlock:^(NSNotification *){
    // Your notification will be processed in this block, and it will be
    // received on the notification queue you specify, regardless of what
    // thread the sender was running when the notification was posted.
}];

メインスレッドで確実に受信したい場合は、使用できます

[NSOperationQueue mainQueue]
于 2012-08-06T17:57:50.097 に答える
2

UI を更新するには、次のようにバックグラウンド スレッドからメイン スレッドで関数を呼び出す必要があります。

[self performSelectorOnMainThread:@selector(updateFunction) withObject:nil waitUntilDone:NO];

変数を渡す必要がある場合は、先に進んでそれらを-withObject

于 2012-08-06T15:55:12.893 に答える