由于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