【问题标题】:does NSMetadataQueryDidUpdateNotification can work with NSFileManager setUbiquitousNSMetadataQueryDidUpdateNotification 是否可以与 NSFileManager setUbiquitous 一起使用
【发布时间】:2014-04-10 09:08:05
【问题描述】:

我正在尝试使用 NSMetadataQueryDidUpdateNotification 跟踪我的 icloud 上传进度..但它无法工作...我不知道问题是什么..

这是我上传到 icloud 的代码

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    [fileCoordinator coordinateReadingItemAtURL:backupUrl options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL *newURL) {
        NSFileManager*  fm = [NSFileManager defaultManager];
        NSError *theError = nil;

        BOOL success =[fm setUbiquitous:YES itemAtURL:backupUrl destinationURL:[[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:bName] error:&theError];

        if (!(success)) {
            [progView dismiss];
            UIAlertView* alertFail=[[UIAlertView alloc]initWithTitle:@"Backup Error" message:@"Could not backup to iCloud." delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertFail show];
            NSLog(@"iCloud error: %@", [theError localizedDescription]);
        }
        else{
            [self loadNotes:bName];
        }
    }];
});

以及用于跟踪我的上传进度的代码

- (void)loadNotes:(NSString *)bname {
self.alertQuery = [[NSMetadataQuery alloc] init];
[self.alertQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, bname]];
[self.alertQuery setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(liveupdate:) name:NSMetadataQueryDidUpdateNotification object:self.alertQuery];
[self.alertQuery startQuery];
}

-(void)liveupdate:(NSNotification *)note {
    NSMetadataQuery* query=[note object];
    if ([query resultCount]==0){
        return;
    }
    NSMetadataItem* item=[query resultAtIndex:0];
    float progress=[[item valueForAttribute:NSMetadataUbiquitousItemPercentUploadedKey]floatValue];

[progView.progBar setProgress:progress animated:NO];

if ([[item valueForAttribute:NSMetadataUbiquitousItemIsUploadedKey] boolValue]){
    [query stopQuery];
    [query disableUpdates];
    _alertQuery=nil;
    [progView dismiss];
}
}

代码有什么问题...

谁能告诉我在 NSFileManager setUbiquitous 中跟踪 icloud 上传进度的最佳方法是什么......

谢谢...

【问题讨论】:

  • 当您向 iCloud 写入数据时,您不需要使用协调写入功能。并不是说这解决了上传状态跟踪。

标签: icloud nsfilemanager


【解决方案1】:

您可能希望观察首先触发的NSMetadataQueryDidFinishGatheringNotification 通知以及初始结果集。

但即便如此,您也可能得不到您想要的,因为更新通知只会在结果集发生变化时触发。您正在搜索特定文件,并且由于该文件没有被删除或类似的东西,因此即使文件上传或下载,您的结果集也将保持不变。

根据我的经验,NSMetadataQuery 对于监控上传和下载进度不是很有效。你可以破解它几乎可以工作,但它永远不是你想要的。

您能做的最好的事情可能是触发元数据查询,观察完成收集的通知,停止查询,然后重新开始查询。每隔一秒左右定期执行此操作,您应该能够跟踪进度。

您还应该考虑是否真的要跟踪单个文件的进度。这将取决于您的文件有多大。在许多情况下,您最好跟踪要上传/下载的文件数或剩余的总字节数。

如果是这种情况,您可以尝试设置包含谓词的元数据,其中包含上传/下载状态。当文件完成上传/下载时,这将持续触发通知。你可以找到这个here 的一个例子。寻找方法startMonitoringMetadata

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 2016-04-01
    • 2011-01-20
    • 2018-08-11
    • 2021-08-05
    • 2019-03-18
    • 2014-03-15
    • 2021-03-31
    • 2020-12-22
    相关资源
    最近更新 更多