尽管数据成功同步,NSMetadataQuery表示没有文档?

时间:2013-06-16 20:56:32

标签: cocoa nsnotifications nsmetadataquery

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

CloudDocument.h:

@property (nonatomic, strong) NSMetadataQuery *alertQuery;

CloudDocument.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走上正轨并告诉我何时对我的文件进行了编辑?我正在使用以下方法保存文件btw:

+(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,而且似乎都没有正常工作。

另外,我绝对是初学者,因此在提出建议时,代码片段和示例非常非常受欢迎!

1 个答案:

答案 0 :(得分:0)

好吧,我找到了答案:我一直在将文件保存到根文件夹,但在NSMetadataQueryUbiquitousDocumentsScope内搜索。应该是NSMetadataQueryUbiquitousDataScope,现在看起来效果很好。我希望这有助于将来的人们!