iOSのGCDを介してメインキューにコードをディスパッチしようとしていますが、最も単純なテストでさえ常に失敗します。結局、それはこれに要約されます:
static const int TICK_INTERVAL = 1;
#pragma UIApplicationDelegate implementation
- (void) doTick
{
if (![NSThread isMainThread])
{
NSLog(@"Trying to dispatch . . .");
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"test . . .");
});
}
}
- (void) startUpdate
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
timer_ = [NSTimer
scheduledTimerWithTimeInterval:TICK_INTERVAL
target:self
selector:@selector(backgroundUpdate)
userInfo:nil
repeats:NO
];
[[NSRunLoop currentRunLoop]
addTimer:timer_
forMode:NSRunLoopCommonModes
];
[[NSRunLoop currentRunLoop] run];
});
UIBackgroundTaskIdentifier back =
[[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[self doTick];
[[UIApplication sharedApplication]
endBackgroundTask:back
];
}];
}
-(void)backgroundUpdate
{
[self doTick];
UIBackgroundTaskIdentifier back =
[[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[self doTick];
[[UIApplication sharedApplication]
endBackgroundTask:back
];
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
timer_ = [NSTimer
scheduledTimerWithTimeInterval:TICK_INTERVAL
target:self
selector:@selector(backgroundUpdate)
userInfo:nil
repeats:NO
];
[[NSRunLoop currentRunLoop]
addTimer:timer_
forMode:NSRunLoopCommonModes
];
[[NSRunLoop currentRunLoop] run];
});
}
- (id) init
{
self = [super init];
[self startUpdate];
return self;
}
それは私のAppDelegate
です。上記のテストテキストをログに記録するためにメインスレッドでNSLogが実行されることを期待しますが、何も起こりません。dispatch_sync
コードは永久に待機し、ブロック内に配置したブレークポイントに到達することはありません。
コードがメインスレッドで実行されないようにしました。でテストする前に、アプリでdispatch_sync
実験しdispatch_async
ました。もちろん、結果は基本的に同じです。何も起こりません(ブロックなし)。
興味深いことに、メインキューでは機能しないようで、他のキュー(現在のキュー、グローバルキュー)は問題なく機能しているように見えます。
意味がある場合は、アプリでPhonegap(Cordova)を使用しています。
何かアイデアはありますか?
どうもありがとう!