NSFileHandle readInBackgroundAndNotify does not work

折月煮酒 提交于 2019-12-24 08:53:51

问题


Hi I'm using NSFileHandle's readInBackgroundAndNotify method to get notifications when a log file has been updated.

I have the following code:

- (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");
}

However the selector is never called and the notification is not received.


回答1:


  1. Add an NSLog to startReading to make sure that's getting called.
  2. Log fh. My guess is that it's nil (most probably because you haven't created MyTestApp.log yet).


来源:https://stackoverflow.com/questions/1324152/nsfilehandle-readinbackgroundandnotify-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!