2009-08-24 46 views
1

您好我正在使用NSFileHandle的readInBackgroundAndNotify方法來獲取更新日誌文件時的通知。NSFileHandle readInBackgroundAndNotify不起作用

我有以下代碼:

- (void)startReading 
{ 
    NSString *logPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Logs/MyTestApp.log"]; 
    NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:logPath]; 
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; 
    [notificationCenter addObserver:self 
          selector:@selector(getData:) 
           name:NSFileHandleReadCompletionNotification 
          object:fh]; 
    [fh readInBackgroundAndNotify]; 
} 

- (void) getData: (NSNotification *)aNotification 
{ 
    NSLog(@"notification received"); 
} 

但是選擇,不會被調用的通知沒有收到。

回答

3
  1. 添加到NSLog的startReading確保天色調用。
  2. 日誌fh。我的猜測是它是nil(很可能是因爲你還沒有創建MyTestApp.log)。
+0

感謝您的回覆:) startReading確實被調用。我NSLogged the fileHandle,這就是它返回: indragie 2009-08-24 23:13:23

+0

解決了我的問題。工作表窗口阻塞了運行循環。 – indragie 2009-08-26 17:50:24