NSFileHandle readInBackgroundAndNotify不起作用 [英] NSFileHandle readInBackgroundAndNotify does not work
本文介绍了NSFileHandle readInBackgroundAndNotify不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用NSFileHandle的readInBackgroundAndNotify方法在更新日志文件时获取通知.
Hi I'm using NSFileHandle's readInBackgroundAndNotify method to get notifications when a log file has been updated.
我有以下代码:
- (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.
推荐答案
- 将NSLog添加到
startReading
以确保被调用. - 登录
fh
.我的猜测是它是nil
(很可能是因为您尚未创建MyTestApp.log).
- Add an NSLog to
startReading
to make sure that's getting called. - Log
fh
. My guess is that it'snil
(most probably because you haven't created MyTestApp.log yet).
这篇关于NSFileHandle readInBackgroundAndNotify不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文