title: How do I determine if file exists in iCloud folder?
body: <p>I have an iOS app that stores files in iCloud. When I start up the app I want to determine if a previous device has already uploaded any files. I start the first device and it adds the files to iCloud (I can see them in the Mobile Documents folder on my Mac). I then start the app on a second device and try to use the following NSMetadataQuery to see if any files have been uploaded, but it returns 0 results. If I keep running that query, after about 8-10 seconds it does return results. </p>
  
  <pre><code>iCloudQuery = [[NSMetadataQuery alloc] init];
  
  iCloudQuery.searchScopes = @[NSMetadataQueryUbiquitousDataScope];
  
  NSString *filePattern = [NSString stringWithFormat:@"*.%@", @"txt"];
  
  iCloudQuery.predicate = [NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, filePattern];
  
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iCloudQueryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:iCloudQuery];
  
  [iCloudQuery startQuery];
  </code></pre>
  
  <p>When I get the notification resultCount is 0 for the query</p>
  
  <pre><code>- (void)iCloudQueryDidFinishGathering:(NSNotification *)notification
  {    
      NSMetadataQuery *query = [notification object];
      [query disableUpdates];
      [query stopQuery];
  
      NSLog(@"Found %d results from metadata query", query.resultCount);
  }
  </code></pre>
  
  <p>Shouldn't NSMetadataQuery return a resultCount if the file exists in iCloud, even if it hasn't been downloaded? Is there any way to test if a file exists other than trying over an over and timing out after 15-30 seconds?</p>