【发布时间】:2013-06-12 08:52:12
【问题描述】:
我正在尝试在 OS X 应用程序上使用NSMetadataQueryDidUpdateNotification 启动并运行,以便在我的 iCloud 通用容器中的文件更新时提醒我。我一直在做很多研究(包括阅读其他 Stack 答案,如 this、this、this 和 this),但我似乎仍然不太正确。
我有一个从 NSDocument 子类化的“CloudDocument”对象,它在 H: 中包含此代码:
@property (nonatomic, strong) NSMetadataQuery *alertQuery;
这是M文件:
@synthesize alertQuery;
-(id)init {
self = [super init];
if (self) {
if (alertQuery) {
[alertQuery stopQuery];
} else {
alertQuery = [[NSMetadataQuery alloc]init];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidUpdate:) name:NSMetadataQueryDidUpdateNotification object:nil];
NSLog(@"Notification created");
[alertQuery startQuery];
}
return self;
}
-(void)queryDidUpdate:(NSNotification *)notification {
NSLog(@"Something changed!!!");
}
根据我的最佳理解,如果一个预先存在的查询正在运行,那么它应该停止一个预先存在的查询,设置一个对无处不在容器的更改的通知,然后启动查询,以便它从这里开始监视更改。
除了,显然不是这样,因为我在启动登录时得到Notification created,但在我更改 iCloud 文档时从未得到Something changed!!!。
谁能告诉我我错过了什么?如果你超级棒,你会帮我提供一些代码示例和/或教程吗?
编辑:如果它很重要/有帮助,我的 ubiquity 容器中只有一个文件正在同步。它被称为“笔记”,所以我使用来自以下位置的 URL 结果访问它:
+(NSURL *)notesURL {
NSURL *url = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
return [url URLByAppendingPathComponent:kAllNotes];
}
其中“kAllNotes”设置为#define kAllNotes @"notes"。
编辑#2:通过我与 Daij-Djan 的对话,我的代码有很多更新,所以这是我的更新代码:
@synthesize alertQuery;
-(id)init {
self = [super init];
if (self) {
alertQuery = [[NSMetadataQuery alloc] init];
if (alertQuery) {
[alertQuery setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSString *STEDocFilenameExtension = @"*";
NSString* filePattern = [NSString stringWithFormat:@"*.%@", STEDocFilenameExtension];
[alertQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, filePattern]];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidUpdate:) name:NSMetadataQueryDidFinishGatheringNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidUpdate:) name:NSMetadataQueryDidUpdateNotification object:nil];
NSLog(@"Notification created");
[alertQuery startQuery];
}
return self;
}
-(void)queryDidUpdate:(NSNotification *)notification {
[alertQuery disableUpdates];
NSLog(@"Something changed!!!");
[alertQuery enableUpdates];
}
【问题讨论】:
标签: xcode cocoa icloud nsmetadataquery