質問

私は(基本的に)iOS 4にバックグラウンドタイマーを作成する必要があります。これにより、特定の時間が経過したときにコードを実行できるようになります。私はあなたがいくつかを使用してこれを達成できることを読んでいます [NSThread detachNewThreadSelector: toTarget: withObject:]; しかし、それは実際にどのように機能しますか?スレッドもバックグラウンドに残ることを確認するにはどうすればよいですか。ローカル通知が行われます いいえ ユーザーに通知するのではなく、コードを実行する必要があるため、私のために働きます。

助けていただければ幸いです!

役に立ちましたか?

解決

それらの呼び出しを使用して、新しいスレッド(DetachNewthRed)にあるパラメーター(Objectを使用)を使用して、オブジェクト(TOTARGET)のメソッド(Selector)を実行できます。

遅延タスクを実行したい場合は、最良のアプローチかもしれません performSelector: withObject: afterDelay: そして、あなたがバックグラウンドコールでタスクを実行したい場合 detachNewThreadSelector: toTarget: withObject:

他のヒント

Grand Central Dispatch(GCD)を使用してこれを行うこともできます。これにより、ブロックを使用してコードを1つの場所に保持できます。また、バックグラウンド処理が終了したらUIを更新する必要がある場合は、メインスレッドをもう一度呼び出すことができます。これが基本的な例です。

#import <dispatch/dispatch.h>

…

NSTimeInterval delay_in_seconds = 3.0;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, delay_in_seconds * NSEC_PER_SEC);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

UIImageView *imageView = tableViewCell.imageView;

// ensure the app stays awake long enough to complete the task when switching apps
UIBackgroundTaskIdentifier taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:{}];

dispatch_after(delay, queue, ^{
    // perform your background tasks here. It's a block, so variables available in the calling method can be referenced here.        
    UIImage *image = [self drawComplicatedImage];        
    // now dispatch a new block on the main thread, to update our UI
    dispatch_async(dispatch_get_main_queue(), ^{        
      imageView.image = image;
      [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
    });
}); 

グランドセントラルディスパッチ(GCD)リファレンス:http://developer.apple.com/library/ios/#documentation/performance/reference/gcd_libdispatch_ref/reference/reference.html

ブロック参照:http://developer.apple.com/library/ios/#featuredarticles/short_practical_guide_blocks/index.html%23//apple_ref/doc/uid/tp40009758

バックグラウンドタスクリファレンス:http://developer.apple.com/library/ios/documentation/uikit/reference/uiapplication_class/reference/reference.html#//apple_ref/occ/instm/uiapplication/beginbackgroundtaskwithexpirationhandler:

これらの提案された方法は、最初にバックグラウンド実行(uibackgroundmodeを使用)にアプリケーションが有効になっている場合にのみ適用可能ですか?

アプリケーションがVoIP/Music/Location Awareアプリであると合法的に主張できない場合、ここで説明されていることを実施する場合、時間間隔が期限切れになったときに実行されませんか?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top