为什么NSFilePresenter协议方法永远不会被调用?

时间:2014-03-05 04:05:48

标签: macos nsfilepresenter

我正在尝试监视本地和iCloud目录中的文件更改并已实现NSFilePresenter协议方法,但唯一被调用的方法是presentedItemAtURL

我是否正确地假设我应该能够监控本地或iCloud目录,并在任何进程添加,修改或删除目录中的文件时随时收到通知。

以下是OS X App的基本代码:

- (void)awakeFromNib {
        _presentedItemURL = myDocumentsDirectoryURL;
        _presentedItemOperationQueue = [[NSOperationQueue alloc] init];
        [_presentedItemOperationQueue setMaxConcurrentOperationCount: 1];
        _fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
}
- (NSURL*) presentedItemURL {
    FLOG(@" called %@", _presentedItemURL);
    return _presentedItemURL;
}

- (NSOperationQueue*) presentedItemOperationQueue {
    FLOG(@" called");
    return _presentedItemOperationQueue;
}

- (void)presentedItemDidChange {
    FLOG(@" called");
    dispatch_async(dispatch_get_main_queue(), ^{
        [self reloadData];
    });
}
-(void)accommodatePresentedItemDeletionWithCompletionHandler:(void (^)(NSError *errorOrNil))completionHandler
{   FLOG(@" called");
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self reloadData];
    }];
    completionHandler(nil);
}
-(void)presentedSubitemDidChangeAtURL:(NSURL *)url {
    FLOG(@" called");
    dispatch_async(dispatch_get_main_queue(), ^{
        [self reloadData];
    });
}
-(void)presentedSubitemDidAppearAtURL:(NSURL *)url {
    FLOG(@" called");
    dispatch_async(dispatch_get_main_queue(), ^{
        [self reloadData];
    });

}

3 个答案:

答案 0 :(得分:3)

很久以前,我知道,但也许这仍然会有所帮助。 NSFilePresenter仅通知您有关对目录或文件进行更改的其他进程所做的更改使用NSFileCoordinator。如果另一个进程(例如:iTunes文件共享)在没有NSFileCoordinator的情况下进行更改,则无法通知您。

答案 1 :(得分:0)

这绝不是我的最终实现,我会在改进时编辑/更新。但是由于没有关于如何做到这一点的例子,我想我会分享一些有用的东西!没错,它有效。我能够在我的应用程序中读取该文件,同时对textedit进行更改,并将更改传播到我的应用程序。希望这有助于萌芽。

PBDocument.h

@interface PBDocument : NSObject <NSFilePresenter>

@property (nonatomic, strong) NSTextView *textView;

#pragma mark - NSFilePresenter properties
@property (readonly) NSURL *presentedItemURL;
@property (readonly) NSOperationQueue *presentedItemOperationQueue;

- (instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError *__autoreleasing *)outError textView:(NSTextView*)textView;

@end

PBDocument.m

@interface PBDocument ()

@property (readwrite) NSURL *presentedItemURL;
@property (readwrite) NSOperationQueue *presentedItemOperationQueue;
@property (readwrite) NSFileCoordinator *fileCoordinator;

@end

@implementation PBDocument

- (instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError *__autoreleasing *)outError textView:(NSTextView*)textView {
    self = [super init];
    if (self) {
        _textView = textView;
        _presentedItemURL = url;

        _presentedItemOperationQueue = [NSOperationQueue mainQueue];

        [NSFileCoordinator addFilePresenter:self];
        _fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];

        [self readWithCoordination];
    }
    return self;
}

- (void)readWithCoordination {
    NSError *error = nil;

    [self.fileCoordinator coordinateReadingItemAtURL:_presentedItemURL options:NSFileCoordinatorReadingWithoutChanges error:&error byAccessor:^(NSURL *newURL) {
        NSLog(@"Coordinating Read");
        NSError *error = nil;
        NSFileWrapper *wrapper = [[NSFileWrapper alloc] initWithURL:newURL options:0 error:&error];

        if (!error) {
            [self readFromFileWrapper:wrapper ofType:[self.presentedItemURL pathExtension] error:&error];
        }

        if (error) @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"%@", error] userInfo:nil];
    }];

    if (error) @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"%@", error] userInfo:nil];
}

- (void)presentedItemDidChange {
    [self readWithCoordination];
}

@end

答案 2 :(得分:0)

如果对任何人有帮助,这就是方法(FSEvents)我最近最终使用了文件同步解决方案,它似乎适用于任何文件系统。我最近没有对NSFileCoordinator进行任何研究,以了解这是更好还是更糟,或者作为比较的用例。

我也没有测试每个用例,因此您的工作量可能会有所不同。

https://github.com/eonil/FSEvents

相关问题