Xcode 为简单响应设置NSMetadataQueryIDUpdateNotification
我正在尝试在OS X应用程序上启动并运行一个Xcode 为简单响应设置NSMetadataQueryIDUpdateNotification,xcode,cocoa,icloud,nsmetadataquery,Xcode,Cocoa,Icloud,Nsmetadataquery,我正在尝试在OS X应用程序上启动并运行一个nsMetadataQueryIDUpdateNotification,以便在iCloud ubiquity容器中的文件更新时提醒我。我已经做了很多研究(包括阅读其他堆栈答案,如、、和),但我似乎仍然没有完全正确的答案 我从NSDocument中得到了一个子类为“CloudDocument”的对象,它在H中包含以下代码: @property (nonatomic, strong) NSMetadataQuery *alertQuery; 这是M文件:
nsMetadataQueryIDUpdateNotification
,以便在iCloud ubiquity容器中的文件更新时提醒我。我已经做了很多研究(包括阅读其他堆栈答案,如、、和),但我似乎仍然没有完全正确的答案
我从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!!!");
}
根据我的理解,如果一个已经存在的查询正在运行,那么应该停止该查询,设置对ubiquity容器的更改通知,然后启动该查询,以便它从现在开始监视更改
除此之外,显然情况并非如此,因为我在启动时的日志中创建了通知
,但从未发生任何更改代码>当我更改iCloud文档时
谁能告诉我我错过了什么?如果你特别棒,你会帮我做一些代码示例和/或教程吗
编辑:如果有必要/有帮助,我的ubiquity容器中只有一个文件正在同步。它被称为“notes”,因此我使用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];
}
您从未分配您的alertQuery
在需要分配的地方,初始化NSMetaDataQuery
例子
}
processFiles方法
==您的QueryIDUpdate
- processFiles(NSNotification*)note {
[aQuery disableUpdates];
.....
[aQuery enableUpdates];
}
注意:禁用并重新启用搜索强>
见:
如何保存您的文档-您提供了什么url?除非您自己给它一个扩展名,否则不会自动给它一个扩展名-因此您的*.
模式将永远不会匹配没有扩展名的文件。尝试*
作为模式,看看会发生什么
此外,它还有助于记录queryDidUpdate中发生的事情,直到您准确了解发生了什么:
尝试以下方法:
-(void)queryDidUpdate:(NSNotification *)notification {
[alertQuery disableUpdates];
NSLog(@"Something changed!!!");
// Look at each element returned by the search
// - note it returns the entire list each time this method is called, NOT just the changes
int resultCount = [alertQuery resultCount];
for (int i = 0; i < resultCount; i++) {
NSMetadataItem *item = [alertQuery resultAtIndex:i];
[self logAllCloudStorageKeysForMetadataItem:item];
}
[alertQuery enableUpdates];
}
- (void)logAllCloudStorageKeysForMetadataItem:(NSMetadataItem *)item
{
NSNumber *isUbiquitous = [item valueForAttribute:NSMetadataItemIsUbiquitousKey];
NSNumber *hasUnresolvedConflicts = [item valueForAttribute:NSMetadataUbiquitousItemHasUnresolvedConflictsKey];
NSNumber *isDownloaded = [item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey];
NSNumber *isDownloading = [item valueForAttribute:NSMetadataUbiquitousItemIsDownloadingKey];
NSNumber *isUploaded = [item valueForAttribute:NSMetadataUbiquitousItemIsUploadedKey];
NSNumber *isUploading = [item valueForAttribute:NSMetadataUbiquitousItemIsUploadingKey];
NSNumber *percentDownloaded = [item valueForAttribute:NSMetadataUbiquitousItemPercentDownloadedKey];
NSNumber *percentUploaded = [item valueForAttribute:NSMetadataUbiquitousItemPercentUploadedKey];
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
BOOL documentExists = [[NSFileManager defaultManager] fileExistsAtPath:[url path]];
NSLog(@"isUbiquitous:%@ hasUnresolvedConflicts:%@ isDownloaded:%@ isDownloading:%@ isUploaded:%@ isUploading:%@ %%downloaded:%@ %%uploaded:%@ documentExists:%i - %@", isUbiquitous, hasUnresolvedConflicts, isDownloaded, isDownloading, isUploaded, isUploading, percentDownloaded, percentUploaded, documentExists, url);
}
-(void)queryIDUpdate:(NSNotification*)通知{
[alertQuery disableUpdates];
NSLog(@“发生了变化!!!”);
//查看搜索返回的每个元素
//-注意,每次调用此方法时,它都会返回整个列表,而不仅仅是更改
int resultCount=[alertQuery resultCount];
for(int i=0;i
太好了,谢谢!我开始理解一切,但我得到一个错误:使用未声明的标识符“STEDocFilenameExtension”。
这是我应该预先声明的还是什么?您附加的链接似乎将其视为给定的,尽管这篇文章是针对iOS而不是OS X的。是否有与Cocoa等效的内容,或者我可以采取的其他步骤?我从中了解到,他们刚刚将STEDocFilenameExtension
设置为“stedoc”,所以我也这样做了。代码现在没有错误,并且与您附加的链接中的演练紧密匹配,但仍然不起作用。。。(再次感谢您的帮助,我知道,我现在很不开心。)stedoc可能只对他们的文件有效。您应该将其设置为用于文件OK的扩展名,以便“DidFinishGatheringNotification”行在启动时调用queryIDUpdate
,但不再调用。在我看来,这是因为我不知道我使用的是什么文件扩展名,因为我是新手,使用了很多教程代码。是否有默认的扩展名,或查找它的方法?我有像[NSKeyedArchiver archivedDataWithRootObject:[Data getAllNotes]]这样的行代码>和[cloudDoc saveTour:[self notesURL]的类型:NSPlainTextDocumentType for saveOperation:NSSaveOperation错误:nil]
和[url URLByAppendingPathComponent:@“notes”]代码>,但我不知道如何理解它们。我循环浏览了我能想到的每个主要文件扩展名(例如plist、txt、dat、data等),以及通过在线搜索找到的每个文件扩展名,我甚至尝试将扩展名保留为星号,希望它适用于所有文件类型的所有文件(因为我只有一个文件需要修改),但我就是找不到回应。每次启动时,它都会调用queryidupdate
方法1-3次,然后再也不会调用了。有什么想法吗?
-(void)queryDidUpdate:(NSNotification *)notification {
[alertQuery disableUpdates];
NSLog(@"Something changed!!!");
// Look at each element returned by the search
// - note it returns the entire list each time this method is called, NOT just the changes
int resultCount = [alertQuery resultCount];
for (int i = 0; i < resultCount; i++) {
NSMetadataItem *item = [alertQuery resultAtIndex:i];
[self logAllCloudStorageKeysForMetadataItem:item];
}
[alertQuery enableUpdates];
}
- (void)logAllCloudStorageKeysForMetadataItem:(NSMetadataItem *)item
{
NSNumber *isUbiquitous = [item valueForAttribute:NSMetadataItemIsUbiquitousKey];
NSNumber *hasUnresolvedConflicts = [item valueForAttribute:NSMetadataUbiquitousItemHasUnresolvedConflictsKey];
NSNumber *isDownloaded = [item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey];
NSNumber *isDownloading = [item valueForAttribute:NSMetadataUbiquitousItemIsDownloadingKey];
NSNumber *isUploaded = [item valueForAttribute:NSMetadataUbiquitousItemIsUploadedKey];
NSNumber *isUploading = [item valueForAttribute:NSMetadataUbiquitousItemIsUploadingKey];
NSNumber *percentDownloaded = [item valueForAttribute:NSMetadataUbiquitousItemPercentDownloadedKey];
NSNumber *percentUploaded = [item valueForAttribute:NSMetadataUbiquitousItemPercentUploadedKey];
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
BOOL documentExists = [[NSFileManager defaultManager] fileExistsAtPath:[url path]];
NSLog(@"isUbiquitous:%@ hasUnresolvedConflicts:%@ isDownloaded:%@ isDownloading:%@ isUploaded:%@ isUploading:%@ %%downloaded:%@ %%uploaded:%@ documentExists:%i - %@", isUbiquitous, hasUnresolvedConflicts, isDownloaded, isDownloading, isUploaded, isUploading, percentDownloaded, percentUploaded, documentExists, url);
}