首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么NSFilePresenter协议方法从不被调用?

为什么NSFilePresenter协议方法从不被调用?
EN

Stack Overflow用户
提问于 2014-03-05 12:05:48
回答 3查看 1.7K关注 0票数 5

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

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

以下是OS X应用程序的基本代码:

代码语言:javascript
复制
- (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];
    });

}
EN

回答 3

Stack Overflow用户

发布于 2017-03-07 15:55:05

我知道这是很久以前的事了,但也许这仍然会有帮助。NSFilePresenter只会通知您另一个进程所做的更改,该进程使用NSFileCoordinator对目录或文件进行更改。如果另一个进程(例如: iTunes文件共享)在没有NSFileCoordinator的情况下进行了更改,您将不会收到通知。

票数 4
EN

Stack Overflow用户

发布于 2014-03-18 10:34:53

这绝不是我的最终实现,我会随着我的改进而编辑/更新。但是由于没有关于如何做到这一点的例子,我想我应该分享一些有效的东西!没错,它起作用了。我可以读取我的应用程序中的文件,同时在textedit中进行更改,这些更改会传播到我的应用程序中。希望这能帮到巴德。

PBDocument.h

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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
票数 0
EN

Stack Overflow用户

发布于 2019-12-05 07:50:56

如果对任何人有任何帮助的话,这就是我最近结束使用的文件同步解决方案的方法(FSEvents),它似乎适用于任何文件系统。我最近没有在NSFileCoordinator上做任何研究,看看这是不是更好,更糟糕,或者是什么用例作为比较。

我也没有测试每个用例,所以您的里程数可能会有所不同。

https://github.com/eonil/FSEvents

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22188312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档