2011-11-07 65 views
2

爲什麼我得到這個代碼的「wait_fences:無法接收回復」?這是我使用通知返回主線程的方式嗎?爲什麼我得到這個代碼的「wait_fences:未能收到回覆」?

#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 
+0

檢查SO帖子... http://stackoverflow.com/questions/4241894/wait-fences-failed-to-receive-reply-10004003-error-uialert-without-uitextfie – Jhaliya

+0

沒有修復它實際上 - 在觸發「ModelChangedHandler」後發生了wait_fences – Greg

回答

2

下面是如何擺脫wait_fences錯誤。更改,您駁回alertView如下使用動畫線:

[self.alert dismissWithClickedButtonIndex:0 animated:YES]; 

我覺得wait_fences事做,可欣賞動畫狀態與警惕的觀點,但很難知道。我認爲這應該消除錯誤味精雖然。我的其他答案不直接擺脫錯誤,但我仍然建議。 UI操作應該在主線程上完成。

+0

是的 - 就是這樣! – Greg

3

這裏有一個明顯的問題。你從後臺線程發佈通知(這很好),這意味着在後臺線程上調用通知處理程序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對象。

+0

感謝 - 有趣的是它似乎沒有解決它......但我明白你的意思,我可以先運行performSelectorOnMainThread,這樣你的代碼纔有意義。只是我仍然在最後得到了「wait_fences」這一行。由此你的信息在這裏有點指出我在這裏得到的迴應可能是錯誤的http://stackoverflow.com/questions/8032652/is-it-ok-to-use-of-a-notification-to-communication-回到主線程的 – Greg

+0

嗯。我寫了兩個答案。我不認爲他們是矛盾的。 wait_fences錯誤可能是由我的評論引起的:re:需要在主線程中的線程和UI操作是正確的。 – XJones

相關問題