NSFilePresenter方法永远不会被调用 [英] NSFilePresenter methods never get called

查看:168
本文介绍了NSFilePresenter方法永远不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的(玩具)程序,该程序使用NSFilePresenter和NSFileCoordinator方法来监视文件中的更改.

I'm trying to write a simple (toy) program that uses the NSFilePresenter and NSFileCoordinator methods to watch a file for changes.

该程序由一个加载了(硬编码)文本文件的文本视图和一个将对文件进行任何更改保存的按钮组成.我的想法是,我有两个实例正在运行,并且保存在一个实例中会导致另一个实例重新加载更改后的文件.

The program consists of a text view that loads a (hardcoded) text file and a button that will save the file with any changes. The idea is that I have two instances running and saving in one instance will cause the other instance to reload the changed file.

加载和保存文件可以正常工作,但是从不调用NSFilePresenter方法.所有这些都基于一个名为FileManager的类,该类实现了NSFilePresenter协议.代码如下:

Loading and saving the file works fine but the NSFilePresenter methods are never called. It is all based around a class called FileManager which implements the NSFilePresenter protocol. The code is as follows:

接口:

@interface FileManager : NSObject <NSFilePresenter>
@property (unsafe_unretained) IBOutlet NSTextView *textView;

- (void) saveFile;
- (void) reloadFile;

@end

实施:

@implementation FileManager
{
    NSOperationQueue* queue;
    NSURL* fileURL;
}

- (id) init {
    self = [super init];
    if (self) {
        self->queue = [NSOperationQueue new];
        self->fileURL = [NSURL URLWithString:@"/Users/Jonathan/file.txt"];
        [NSFileCoordinator addFilePresenter:self];
    }
    return self;
}

- (NSURL*) presentedItemURL {
    NSLog(@"presentedItemURL");
    return self->fileURL;
}

- (NSOperationQueue*) presentedItemOperationQueue {
    NSLog(@"presentedItemOperationQueue");
    return self->queue;
}

- (void) saveFile {
    NSFileCoordinator* coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
    NSError* error;
    [coordinator coordinateWritingItemAtURL:self->fileURL options:NSFileCoordinatorWritingForMerging error:&error byAccessor:^(NSURL* url) {
        NSString* content = [self.textView string];
        [content writeToFile:[url path] atomically:YES encoding:NSUTF8StringEncoding error:NULL];
    }];
}

- (void) reloadFile {
    NSFileManager* fileManager = [NSFileManager defaultManager];
    NSFileCoordinator* coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
    NSError* error;
    __block NSData* content;
    [coordinator coordinateReadingItemAtURL:self->fileURL options:0 error:&error byAccessor:^(NSURL* url) {
        if ([fileManager fileExistsAtPath:[url path]]) {
            content = [fileManager contentsAtPath:[url path]];
        }
    }];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.textView setString:[[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding]];
    });
}

// After this I implement *every* method in the NSFilePresenter protocol. Each one
// simply logs its method name (so I can see it has been called) and calls reloadFile
// (not the correct implementation for all of them I know, but good enough for now).

@end

注意,在applicationDidFinishLaunching中调用reloadFile,并且每次单击保存按钮(通过应用程序委托)时都会调用saveFile.

Note, reloadFile is called in applicationDidFinishLaunching and saveFile gets called every time the save button is click (via the app delegate).

唯一调用过的NSFilePresenter方法(通过日志记录)是presentItemURL(在程序启动并加载文件时调用四次,在每次单击保存时调用三遍.在第二个实例中单击保存"没有引起注意.对初审产生影响.

The only NSFilePresenter method that ever gets called (going by the logs) is presentedItemURL (which gets called four times when the program starts and loads the file and three times whenever save is clicked. Clicking save in a second instance has no noticeable effect on the first instance.

有人可以告诉我我在做什么错吗?

Can anyone tell me what I'm doing wrong here?

推荐答案

一段时间以来,我一直在努力解决这个确切的问题.对我来说,唯一可以调用的方法是-presentedSubitemDidChangeAtURL:(我正在监视目录而不是文件).我向Apple提出了一个技术支持问题,他们的回答是这是一个错误,如果您要监视目录,我们现在唯一能做的就是通过-presentedSubitemDidChangeAtURL:进行所有操作.不确定监视文件时可以做什么.

I was struggling with this exact issue for quite a while. For me, the only method that would be called was -presentedSubitemDidChangeAtURL: (I was monitoring a directory rather than a file). I opened a technical support issue with Apple, and their response was that this is a bug, and the only thing we can do right now is to do everything through -presentedSubitemDidChangeAtURL: if you're monitoring a directory. Not sure what can be done when monitoring a file.

我鼓励遇到此问题的任何人提交错误( https://bugreport.apple.com )鼓励苹果尽快解决此问题.

I would encourage anyone encountering this issue to file a bug (https://bugreport.apple.com) to encourage Apple to get this problem fixed as soon as possible.

这篇关于NSFilePresenter方法永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆