设置NSMetadataQueryDidUpdateNotification以获得简单响应 [英] Setting up NSMetadataQueryDidUpdateNotification for a simple response

查看:127
本文介绍了设置NSMetadataQueryDidUpdateNotification以获得简单响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在OS X应用程序上启动 NSMetadataQueryDidUpdateNotification 并运行,以提醒我iCloud普遍存在容器中的文件已更新。我一直在做很多研究(包括阅读其他Stack答案,例如),但我仍然不太正确,它似乎。

I'm trying to get up and running with an NSMetadataQueryDidUpdateNotification on an OS X app, to alert me when a file in my iCloud ubiquity container is updated. I've been doing a lot of research (including reading other Stack answers like this, this, this, and this), but I still don't have it quite right, it seems.

我从NSDocument继承了一个 CloudDocument对象,该对象在H中包含此代码:

I've got a "CloudDocument" object subclassed from NSDocument, which includes this code in the 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!!!");
}

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

According to my best understanding, that should stop a pre-existing query if one is running, set up a notification for changes to the ubiquity container, and then start the query so it will monitor changes from here on out.

除了,显然,这不是发生这种情况的原因是,在启动日志时我得到了通知创建,但是在更改iCloud文档时却没有发生了什么改变!!

Except, clearly that's not the case because I get Notification created in the log on launch but never Something changed!!! when I change the iCloud document.

有人可以告诉我我在想什么吗?而且,如果您对超级调味酱太棒了,可以通过一些代码示例和/或教程来帮助我吗?

Can anyone tell me what I'm missing? And if you're extra-super-sauce awesome, you'll help me out with some code samples and/or tutorials?

编辑: >如果有关系/有帮助,在我的普遍存在的容器中只有一个文件正在同步。它被称为笔记,因此我使用URL结果从以下位置访问它:

If it matters/helps, there is only one file in my ubiquity container being synced around. It's called "notes", so I access it using the URL result from:

+(NSURL *)notesURL {
    NSURL *url = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    return [url URLByAppendingPathComponent:kAllNotes];
}

其中, kAllNotes设置为 #define kAllNotes @ notes

where "kAllNotes" is set with #define kAllNotes @"notes".

编辑#2:通过我的代码对我的代码进行了大量更新与Daij-Djan的对话,所以这是我的更新代码:

EDIT #2: There have been a lot of updates to my code through my conversation with Daij-Djan, so here is my updated code:

@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];
}


推荐答案

如何保存文档-您提供什么网址?除非您自己给扩展名加上扩展名,否则它不会自动得到一个扩展名-因此您的 *。* 模式将永远不会匹配没有扩展名的文件。尝试使用 * 作为模式,看看会发生什么。

How do you save your document - what url do you give it? Unless you give it an extension yourself, it won't automatically be given one - so your *.* pattern will never match a file that does not have an extension. Try * as the pattern and see what happens.

此外,它有助于记录queryDidUpdate中发生的情况,直到弄清楚到底发生了什么为止:

Also, it helps to log what is happening within queryDidUpdate, until you've worked out exactly what's going on :

尝试类似的操作:

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

这篇关于设置NSMetadataQueryDidUpdateNotification以获得简单响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆