gpt4 book ai didi

xcode - 设置 NSMetadataQueryDidUpdateNotification 以进行简单响应

转载 作者:行者123 更新时间:2023-12-03 16:57:54 24 4
gpt4 key购买 nike

我正在尝试在 OS X 应用程序上启动并运行 NSMetadataQueryDidUpdateNotification,以便在 iCloud 无处不在容器中的文件更新时提醒我。我一直在做很多研究(包括阅读其他堆栈答案,如 thisthisthisthis ),但看来我仍然不太正确。

我有一个从 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!!!");
}

根据我的最佳理解,如果现有查询正在运行,则应停止该查询,为普遍存在的容器的更改设置通知,然后启动查询,以便从现在开始监视更改。

但是,显然情况并非如此,因为我在启动时在日志中收到已创建通知,但当我更改 iCloud 文档时却从未某些内容发生了变化!!!

谁能告诉我我错过了什么?如果您非常出色,您会帮我提供一些代码示例和/或教程吗?

编辑:如果重要/有帮助,我的普遍容器中只有一个文件正在同步。它称为“注释”,因此我使用以下网址结果访问它:

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

最佳答案

如何保存文档 - 您提供什么 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);
}

关于xcode - 设置 NSMetadataQueryDidUpdateNotification 以进行简单响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17129946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com