2014-01-30 2 views
1

NSMetadataQuery을 사용하여 업로드 한 파일의 비율을 icloud로 추적하려고하지만 작동하지 않았습니다.NSMetadataQueryDidUpdateNotification을 호출 할 수 없습니다

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를 메서드를 호출 할 수 있습니다 감사합니다. 내 코드에 어떤 문제가 있습니까?

답변

2

메타 데이터 쿼리에 몇 가지 문제가있는 것 같습니다. 우선 :

[query setPredicate:[NSPredicate predicateWithFormat:@"%K > 0",NSMetadataUbiquitousItemPercentUploadedKey]]; 

아마도 이것이 작동해야 할 수도 있지만, 나는 그것에 대해 회의적입니다. 여기서 정말로 필요한 것은 파일 이름을 사용하는 술어입니다.

[query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, filename]]; 

가장 큰 문제입니다 : 파일 이름이 같은 NSString 이름 filename, 뭔가에 저장됩니다. 그것을 고치는 것은 당신이 필요한 전부일지도 모릅니다. 그러나 내가 바꿀만한 다른 것들이 있습니다. 다음 : 다시

[query setSearchScopes:@[destURL]]; 

, 그 일을 해야하는 것이 될 수도 있지만, 나는 단지 훨씬 더 일반적인 설정으로 좋은 결과를 본 적이 : 마지막으로

[query setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]]; 

:

[query setValueListAttributes:@[NSMetadataUbiquitousItemPercentUploadedKey,NSMetadataUbiquitousItemIsUploadedKey]]; 

valueListAttributes을 통해 쿼리 개체에서 직접 값을 조회 한 경우이 값은 일 수 있습니다. 하지만이 줄을 완전히 버리는 것이 좋습니다. 진행률 및 상태는 여전히 NSMetadataItem에서 찾을 수 있습니다.

+0

내 코드가 업데이트되었지만 여전히 liveupdate 메소드를 호출 할 수 없습니다. – Faiz

관련 문제