2014-03-05 177 views
3

我想监视本地和iCloud的目录文件的更改,并实施了NSFilePresenter协议的方法,但被调用的唯一方法是presentedItemAtURL为什么NSFilePresenter协议方法永远不会被调用?

我是在假设我应该能够监视本地或iCloud的目录,并获得通知任何时候任何过程添加,修改或删除目录中的文件正确。

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

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

} 

回答

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 
+0

不保持URL复制什么NSDocument做呢?与标准方法相比,这在跟踪文件位置更改方面如何进行缩放? – ctietze

+1

是的,跟踪URL复制NSDocument功能。如果我记得正确,当时单个窗口实例代表一个NSDocument(我不记得一个方法),并不符合我的要求(我希望所有文档在侧边栏以分层方式显示)。虽然这在当时起作用,但API已经发生了变化,以至于我的应用程序不再运行,现在可能有更好的方法。这是一个非常漂亮的方式来获得有关文本文件更改的通知,并且在我的应用程序中立即反映了这些更改。 –

3

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

相关问题