由於NSNotification在除主線程以外的線程上調用其選擇器,我注意到您對UIView或其他界面元素作出的任何響應該通知的更改往往生效緩慢。如果主線程很忙(這是我的經常做的),這是最嚴重的。在響應NSNotifications時,更新UIViews的最佳做法是什麼
我可以通過調用「performSelectorOnMainThread:」來解決這個問題。這真的是最佳做法嗎?
- (void) gotMovieSaveFinishNotication: (NSNotification *) not {
NSURL *exportMovieURL = (NSURL *) [not object];
//saving the composed video to the photos album
ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease];
if(![library videoAtPathIsCompatibleWithSavedPhotosAlbum: exportMovieURL]) {
NSLog(@"videoAtPathIsCompatibleWithSavedPhotosAlbum fails for: @",exportMovieURL);
return;
}
[library writeVideoAtPathToSavedPhotosAlbum:exportMovieURL
completionBlock:^(NSURL *assetURL, NSError *error)
{
[self performSelectorOnMainThread:@selector(setTintToNormal)
withObject: NULL
waitUntilDone: YES];
if(error)
{
DLog(@"The video saving failed with the following error =============== %@",error);//notify of completion
}
else
{
DLog(@"The video is saved to the Photos Album successfully");
}
}];
}
感謝您提醒關於UIKit訪問主線程的危險。我已經閱讀過,但不知怎的,從來沒有認真對待:-)。現在我回到了直線和狹窄。 – 2011-03-14 19:34:07
是的,我喜歡那個類別的解決方案。似乎最簡潔的方式,特別是因爲這是發生在多個地方。謝謝您的幫助! – 2011-03-14 19:35:02