2013-10-11 4 views
0

난 내 응용 프로그램에서 iCloud에 UIDocument을 활성화라고하고, 내가 클라우드에 문서를 저장, 문서 내가이 일을하고있어 포장의 아이 클라우드 문서의 업데이트 알림을받을 수 있습니다 :NSMetadataQueryDidUpdateNotification 몇 시간

- (void)startQuery { 

    [self stopQuery]; 

    NSLog(@"Starting to watch iCloud dir..."); 

    _query = [self documentQuery]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(processiCloudFiles:) 
               name:NSMetadataQueryDidFinishGatheringNotification 
               object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(processiCloudFiles:) 
               name:NSMetadataQueryDidUpdateNotification 
               object:nil]; 

    [_query startQuery]; 
} 

- (void)stopQuery { 

    if (_query) { 

     NSLog(@"No longer watching iCloud dir..."); 

     [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:nil]; 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidUpdateNotification object:nil]; 
     [_query stopQuery]; 
     _query = nil; 
    } 

} 

- (NSMetadataQuery *)documentQuery { 

    NSMetadataQuery * query = [[NSMetadataQuery alloc] init]; 
    if (query) { 

     [query setSearchScopes:[NSArray arrayWithObject: 
           NSMetadataQueryUbiquitousDocumentsScope]]; 

     NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K == %@", 
          NSMetadataItemFSNameKey, 
          kFILENAME]; 
     [query setPredicate:pred]; 

    } 
    return query; 

} 

- (void)processiCloudFiles:(NSNotification *)notification { 

    [_query disableUpdates]; 

    if ([_query resultCount] == 1) { 
     NSMetadataItem *item = [_query resultAtIndex:0]; 
     NSURL *url = [item valueForAttribute:NSMetadataItemURLKey]; 

     if (!self.folder) { 
      self.folder = [[MyDocument alloc] initWithFileURL:url]; 
     } 
     [self.folder openWithCompletionHandler:^ (BOOL success) { 
      if (success) { 
       NSLog(@"loadData - doc opened from cloud %i", self.folder.count); 
       [self.folder closeWithCompletionHandler:^(BOOL success) { 
        NSLog(@"doc closed"); 
       } 
       ]; 
      } else { 
       NSLog(@"failed to open"); 
      } }]; 

    } 
} 

내가 문서의 변경을 할 경우 내가 [self startQeury] 전화 모두를 시작하는 데 문제가 NSMetadataQueryDidUpdateNotification 알림 몇 시간이라는 것입니다, 당신은 로그를 볼 수있는 방법 :

2013-10-12 01:43:54.933 Starting to watch iCloud dir... 
2013-10-12 01:43:56.119 loadData - doc opened from cloud 56 
2013-10-12 01:43:56.120 doc closed 
2013-10-12 01:44:01.552 item deleted 

을 당신은 내가 맥을 여기서 볼 수 있듯이 hange를 선택하면 스폰 4 알림을 볼 수 있습니다.

2013-10-12 01:44:08.110 loadData - doc opened from cloud 55 
2013-10-12 01:44:08.111 doc closed 
2013-10-12 01:44:11.942 loadData - doc opened from cloud 55 
2013-10-12 01:44:11.943 doc closed 
2013-10-12 01:44:13.198 loadData - doc opened from cloud 55 
2013-10-12 01:44:13.199 doc closed 
2013-10-12 01:44:14.925 loadData - doc opened from cloud 55 
2013-10-12 01:44:14.926 doc closed 

여러 번 호출되는 이유는 무엇입니까? 내 애플 리케이션에서 업데이 트가 완료되면 변경 사항을 처리해야하므로 알림이 하나만 필요합니다 ... 어떻게 할 수 있습니까? 편집

: 나는이 질문에 대해 답 제안 @rmaddy 어떤 시도가 ,이 시도가 :

if ([[item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey] boolValue]) { 

     if (!self.folder) { 
      self.tvfilesFolder = [[MyDocument alloc] initWithFileURL:url]; 
     } 
     [self.folder openWithCompletionHandler:^ (BOOL success) { 
      if (success) { 
       NSLog(@"loadData - doc opened from cloud %i", self.folder.count); 
       [self.folder closeWithCompletionHandler:^(BOOL success) { 
        NSLog(@"doc closed"); 
       } 
       ]; 
      } else { 
       NSLog(@"failed to open"); 
      } }]; 
    } 

을하지만 다시 그 문제가이 로그입니다 :

2013-10-12 10:35:26.084 Starting to watch iCloud dir... 
2013-10-12 10:35:30.306 loadData - doc opened from cloud 54 
2013-10-12 10:35:30.307 doc closed 

다음 문서를 편집하고 이것은 로그입니다.

2013-10-12 10:35:53.233 loadData - doc opened from cloud 54 
2013-10-12 10:35:53.235 doc closed 
2013-10-12 10:35:56.703 loadData - doc opened from cloud 54 
2013-10-12 10:35:56.704 doc closed 
2013-10-12 10:35:58.489 loadData - doc opened from cloud 54 
2013-10-12 10:35:58.490 doc closed 

내가 어떻게 할 수 있니?

편집 2 :

내가이 함께 시도했다 :

if ([[item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey] boolValue] && [[item valueForAttribute:NSMetadataUbiquitousItemIsUploadedKey] boolValue]) { 
.... 
} 

과 같다 일, 당신이 어떻게 생각? 변경 사항이있을 때마다 처리 할 수 ​​있습니까, 아니면 이런 식으로 일부 알림을 놓칠 수 있습니까?

답변

0

알림은 여러 가지 이유로 호출됩니다. 파일을 iCloud에 처음 업로드 할 때 및 파일을 iCloud에서 다운로드 한 경우. 업로드 및 다운로드 비율에 대한 업데이트도 제공됩니다. 파일의 다양한 속성을 살펴보면이 모든 것을 볼 수 있습니다.

- (void)echoMetadataItem:(NSMetadataItem *)item { 
    NSString *path = [item valueForAttribute:NSMetadataItemPathKey]; 
    NSString *display = [item valueForAttribute:NSMetadataItemDisplayNameKey]; 
    NSURL *url = [item valueForAttribute:NSMetadataItemURLKey]; 
    NSString *name = [item valueForAttribute:NSMetadataItemFSNameKey]; 
    NSNumber *downloaded = [item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey]; 
    NSNumber *downloading = [item valueForAttribute:NSMetadataUbiquitousItemIsDownloadingKey]; 
    NSNumber *uploaded = [item valueForAttribute:NSMetadataUbiquitousItemIsUploadedKey]; 
    NSNumber *uploading = [item valueForAttribute:NSMetadataUbiquitousItemIsUploadingKey]; 
    NSDate *createData = [item valueForAttribute:NSMetadataItemFSCreationDateKey]; 
    NSDate *updateDate = [item valueForAttribute:NSMetadataItemFSContentChangeDateKey]; 
    NSNumber *hasConflicts = [item valueForAttribute:NSMetadataUbiquitousItemHasUnresolvedConflictsKey]; 

    RMLogInfo(@"Metadata item name: %@, display: %@, path: %@, url: %@, ded: %@, ding: %@, ued:%@, uing: %@, created: %@, updated: %@, conflicts: %@", name, display, path, url, downloaded, downloading, uploaded, uploading, createData, updateDate, hasConflicts); 
} 

도 업로드 및 다운로드의 비율을 얻을 수있는 키가 있습니다

나는 각 메타 데이터 항목을 기록하는 약간의 디버깅 방법을 사용합니다.

알림을 한 번 처리하려는 경우 파일이 다운로드되었는지 확인할 수 있습니다.

+0

doens't는 작동합니다. NSMetadataUbiquitousItemIsDownloadedKey는이 키로 시도합니다. 로그와 함께 편집 한 질문을보십시오. – Piero

+0

나는 다시 질문을 수정했습니다 ... – Piero

관련 문제