0

我正在尝试让我的 OS X 应用程序根据对 iCloud 文档的更改自动更新,但我很难让通知正常工作。具体来说,我得到了一些代码,应该列出我的所有结果NSMetadataQuery(这样我就可以进行一些测试和确认),但它返回 0 结果。尽管事实上我的无处不在的容器中显然有一些东西,因为数据正在从我的 OS X 应用程序成功上传,并在启动时再次成功下载。这就是我现在所拥有的。

云文档.h:

@property (nonatomic, strong) NSMetadataQuery *alertQuery;

云文档.m:

@implementation CloudDocument

@synthesize alertQuery;

-(id)init {
    self = [super init];
    if (self) {
        alertQuery = [[NSMetadataQuery alloc] init];
        if (alertQuery) {
            // Search the Documents subdirectory only.
            [alertQuery setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

            // Add a predicate for finding the documents.
            NSString *filePattern = @"*";
            [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!!! - %@", [notification name]);

    // 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 = (int)[alertQuery resultCount];
    NSLog(@"%i", resultCount);
    for (int i = 0; i < resultCount; i++) {
        NSMetadataItem *item = [alertQuery resultAtIndex:i];
        [self logAllCloudStorageKeysForMetadataItem:item];
    }

    [alertQuery enableUpdates];
}

- (void)logAllCloudStorageKeysForMetadataItem:(NSMetadataItem *)item {
    NSLog(@"LogAll gets called");
    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);
}

-(NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
    [Data setSyncProgressIndicators:NO];
    return [NSKeyedArchiver archivedDataWithRootObject:[Data getAllNotes]];
}

-(BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
    [Data setSyncProgressIndicators:YES];
    NSDictionary *dict = (NSDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)data];
    [Data didReceiveCloudData:dict];
    [Data setSyncProgressIndicators:NO];
    return YES;
}

+(BOOL)autosavesInPlace {
    return YES;
}

@end

我看到的结果是queryDidUpdate:在启动时被调用 2-3 次,全部来自NSMetadataQueryDidFinishGatheringNotification. 问题是,“resultCount”变量始终为 0,因此logAllCloudStorageKeysForMetadataItem永远不会被调用,也NSMetadataQueryDidUpdateNotification永远不会被调用,无论我多久编辑一次云文档。

任何人都可以就搜索返回 0 结果的原因提供任何建议,尽管显然有一个文件正在来回同步?或者更好的是,您能完全看到代码有什么问题吗?我可以做些什么来NSMetadataQueryDidUpdateNotification走上正轨并告诉我何时对我的文件进行了编辑?顺便说一句,我正在使用以下方法保存文件:

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

其中“kAllNotes”=“笔记”。

预先感谢您的帮助!

编辑: 请注意,当我说它同步成功时,由于 NSMetadataQuery 而不是这样做的。我在cloudDoc = [[CloudDocument alloc]initWithContentsOfURL:[self notesURL] ofType:NSPlainTextDocumentType error:nil];其他地方有一行加载 CloudDocument,所以我知道那里有一个文档,并且我知道它正在被应用程序的 iOS 版本更改,但我现在试图掌握的 NSMetadataQuery 只是在那里与 一起工作NSMetadataQueryDidUpdateNotification,但似乎都没有正常工作。

另外,我绝对是初学者,因此非常非常感谢提出建议时的代码片段和示例!

4

1 回答 1

0

好的,我找到了答案:我一直将文件保存到根文件夹,但在NSMetadataQueryUbiquitousDocumentsScope. 它应该是NSMetadataQueryUbiquitousDataScope,现在它似乎工作得很好。我希望这对未来的人们有所帮助!

于 2013-06-16T23:54:00.830 回答