2011-11-10 5 views
1

NSViewにN個の画像を描画するタスクがあります。 しかし、私はイメージの量を知らないので、私はそれをバックグラウンドのスレッドで動作するように設定しました。バックグラウンドスレッドの更新に関する通知NSView

[NSThread detachNewThreadSelector:@selector(workInBackgroundThread:) toTarget:self withObject:nil]; 

それは画像の量を得るとき、それは私がNSViewの通知機能

-(void)processGotImagesAmount:(NSNotification*)notification 
{ 

    //others codes 

    [myNSView setFrame:CGRectMake(0, 0, calculateNewWidth, caculatedNewHeight)];//line A 

    //others codes 

} 

の画像の量に依存してサイズを変更しようとする通知

- (void)workInBackgroundThread:(id)unusedObject { 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

//get image amount.... 

[[NSNotificationCenter defaultCenter] postNotificationName:@"gotImageAmount" object:nil]; 

//... 
//continue to load images 

} 
を送信しますアプリがラインAを実行するとフリーズします。

[myNSView setFrame:CGRectMake(0, 0, calculateNewWidth, caculatedNewHeight)]; 

それ自体は問題なく、私はそれを呼び出すためにボトルをクリックすると、動作します!

が、それは、通知機能に

を動作しないように見えますがあなたがバックグラウンドスレッドから通知を投稿している任意のコメント

答えて

7

ようこそ。 NSNotificationCenterドキュメントから

:マルチスレッドアプリケーションで

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html

、通知は常に通知があなたの通知処理コードで

投稿された スレッドに配信されますメインスレッドから行う必要があるuiを更新しています。そのコードを別のメソッドに移動し、通知処理コードでperformSelectorOnMainThreadを使用して呼び出します。

別のオプションは、通知のないGCDです。私はこのS.O.にサンプルを投稿しました。答え:

GCD, Threads, Program Flow and UI Updating

関連する問題