私は、次のディスパッチキューに私のアプリを持っている:メインスレッドの競合/タイミングの問題 - GCD - iPhone
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue,^{
[activeModel freeUpMallocedData];
// UI Updates have to be made on the main thread, so request from GCD.
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue,^{
[mainViewController removeTidyUpScreen];
[mainViewController showSceneList];
[activeModel release];
});
});
freeUpMallocedData方法は、UIの進捗状況表示を更新します。
- (void) freeUpMallocedData {
// Calculate the percentage increase for each item in the pointerStorageArray as it is released so we can update the Progress Screen.
float arrayCount = [pointerStorageArray count];
float incrementCounter = 1/arrayCount; // Caculates 1% of pointerStorageArray
float newValue = incrementCounter;
int stepCounter = 0;
NSString * message;
// Now iterate through the pointerStorageArray and free all malloced memory.
for (NSValue * value in pointerStorageArray) {
stepCounter ++;
newValue = newValue + incrementCounter;
message = [NSString stringWithFormat:@"Freeing Up Memory (%d of %d) ...", stepCounter, (int)arrayCount];
free(value);
[self tidyUpProgress:message amount:newValue];
}
}
tidyUpProgress方法その後、実行をメインスレッドで
- (void) tidyUpProgress: (NSString *) progressMsg amount: (float) amount {
if (tidyUpMonitorDelegate) {
tidyUpProgressMsg = progressMsg;
tidyUpProgressAmount = amount;
[tidyUpMonitorDelegate performSelectorOnMainThread:@selector(model3DTidyUpProgressUpdate) withObject:nil waitUntilDone:NO];
}
}
- (void) model3DTidyUpProgressUpdate {
progressView.progress = app.activeModel.tidyUpProgressAmount;
loadingStatus.text = app.activeModel.tidyUpProgressMsg;
}
freeUpMallocedDataメソッドが完了すると、アプリケーションがクラッシュするという問題があります。これは、私の最初のディスパッチ・キューがメイン・キューを要求するために移動し、アクティブ・ビューを解放するためです。これは、最後のアップデートを実行する前にtidyUpMonitorDelegateからスレッドをハイジャックしているように見えます。メインスレッドを戻すと、activeViewが解放されたため、model3DTidyUpProgresUpdateメソッドが現在のクラスの変数へのアクセスを要求しているので、割り当て解除されました。
誰もこのタイミングの問題を解決する方法についてアドバイスできますか?
ありがとうございます。
どちらですか? tidyUpMonitorDelegateのものか、それとももう1つですか? – GuybrushThreepwood
私はあなたのためのコードを追加しました... –
ああそれは動作します!あなたは天才です - とてもシンプルな何か - ありがとう! – GuybrushThreepwood