Iphone 为什么我会得到;等待“围栏:未能收到答复”;这个代码?
为什么我会收到此代码的“wait_fences:failed to receive reply”?这是我使用通知与主线程通信的方式吗Iphone 为什么我会得到;等待“围栏:未能收到答复”;这个代码?,iphone,ios,nsthread,nsnotifications,nsnotificationcenter,Iphone,Ios,Nsthread,Nsnotifications,Nsnotificationcenter,为什么我会收到此代码的“wait_fences:failed to receive reply”?这是我使用通知与主线程通信的方式吗 #import "ViewController.h" @implementation ViewController @synthesize alert; #pragma mark - Background Thread Test Methods - (void) ConfigTasksForBackground:(id)sender{ NSLo
#import "ViewController.h"
@implementation ViewController
@synthesize alert;
#pragma mark - Background Thread Test Methods
- (void) ConfigTasksForBackground:(id)sender{
NSLog(@"ConfigTasksForBackground - Starting");
[NSThread sleepForTimeInterval:6];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ModelChanged" object:self];
NSLog(@"ConfigTasksForBackground - Ending");
}
#pragma mark - Callbacks
- (void) ModelChangedHandler:(NSNotification *) notification {
if ([[notification name] isEqualToString:@"ModelChanged"]) {
NSLog(@"ModelChangedHandler");
[self.alert dismissWithClickedButtonIndex:0 animated:false];
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(ModelChangedHandler:)
name:@"ModelChanged"
object:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.alert = [[[UIAlertView alloc] initWithTitle:@"Title"
message:@"viewDidAppear"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil] autorelease];
[alert show];
[self performSelectorInBackground:@selector(ConfigTasksForBackground:) withObject:nil];
}
@end
输出为:
2011-11-07 15:15:42.730 test_background[6876:13603] ConfigTasksForBackground - Starting
2011-11-07 15:15:48.734 test_background[6876:13603] ModelChangedHandler
2011-11-07 15:15:49.236 test_background[6876:13603] ConfigTasksForBackground - Ending
wait_fences: failed to receive reply: 10004003
这里有一个明显的问题。您正在从后台线程发布通知(这很好),这意味着正在后台线程上调用通知处理程序
ModelChangedHandler
。然后,处理程序将取消必须在主线程上执行的警报视图。尝试将代码更改为:
- (void) ModelChangedHandler:(NSNotification *) notification {
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(ModelChangedHandler:) withObject:notification waitUntilDone:NO];
}
else if ([[notification name] isEqualToString:@"ModelChanged"]) {
NSLog(@"ModelChangedHandler");
[self.alert dismissWithClickedButtonIndex:0 animated:false];
}
}
编辑:输入太快,更改了答案以反映正确的UI对象。以下是如何消除等待围栏错误。将关闭alertView的行更改为使用动画,如下所示:
[self.alert dismissWithClickedButtonIndex:0 animated:YES];
我认为wait_fences与具有警报视图的视图动画状态有关,但很难确定。我确实认为这应该可以消除错误消息。我的另一个答案并没有直接消除这个错误,但我仍然推荐它。UI操作应该在主线程上完成。检查SO post。。。实际上没有修复它-等待发生在“ModelChangedHandler”触发Hanks之后-有趣的是,它似乎没有修复它…但是我明白你的意思,我可以先运行performSelectorOnMainThread,所以你的代码是有意义的。只是我在最后还有一句“等待”。顺便说一下,你这里的信息表明我在这里得到的回应可能是错误的。我写了两个答案。我认为它们并不矛盾。wait_fences错误可能是由我的注释re:threads引起的,需要在主线程上执行的UI操作是正确的。