1

を使用して icloud にアップロードしたファイルの割合を追跡したいのですが、うまくいきNSMetadataQueryませんでした。

これは私のコードです:

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{
            NSURL* destURL=[[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:bName];
            NSMetadataQuery* query=[[NSMetadataQuery alloc]init];
            [query setPredicate:[NSPredicate predicateWithFormat:@"%K > 0",NSMetadataUbiquitousItemPercentUploadedKey]];
            [query setSearchScopes:@[destURL]];
            [query setValueListAttributes:@[NSMetadataUbiquitousItemPercentUploadedKey,NSMetadataUbiquitousItemIsUploadedKey]];

            _alertQuery=query;
            [query enableUpdates];
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(liveupdate:) name:NSMetadataQueryDidUpdateNotification object:query];
`//                [progView dismiss];
            NSLog(@"desturl %@",query);
            [query  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];
}

}

liveUpdate:noteメソッドは呼び出されませんでした。誰かがこのコードを修正する方法を教えてくれますか? ありがとうございました

コードを編集しました...

これは私の新しいコードです

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

まだ liveupdate メソッドを呼び出すことはできません。私のコードの問題は何ですか?

4

1 に答える 1