My app is using UIDocument + iCloud to create and view documents. The files are stored this way:
- Documents/Filename.db
- Documents/Filename.db/File.db (Filename.db is the directory)
Files inside the Documents/Filename.db directory loads fine at its own View Controller. When opening Documents/Filename.db/File.db inside another View Controller, I'm having problems loading those files. The filenames are not fixed and there are multiple occurrences of them. I can verify that the files are stored inside the directory, so that shouldn't be the problem.
I think I've tracked down the problem to NSMetadataQuery, because it's only searching inside Documents/ - I want it to search inside the directory Documents/Filename.db. I've tried setting the query serachScope to the correct path, but it seems like I need to use NSMetadataQueryUbiquityContainerDocumentsScope for searching to return results.
- (NSMetadataQuery *)documentQuery
{
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
NSString *appending = [NSString stringWithFormat:@"Documents/%@.db/", currentDirectory]; // Directory name
NSURL *documentPath = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:appending];
NSLog(@"Document Path: %@", documentPath);
if (query) {
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSString *filePattern = [NSString stringWithFormat:@"*.%@", STACK_EXTENSION];
[query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, filePattern]];
}
return query;
}
- (void)processiCloudFiles:(NSNotification *)notification
{
[_query disableUpdates];
[_iCloudURLs removeAllObjects];
NSArray *queryResults = [_query results];
for (NSMetadataItem *result in queryResults) {
NSURL *fileURL = [result valueForAttribute:NSMetadataItemURLKey];
NSLog(@"---- FileURL: %@", fileURL);
NSNumber *aBool = nil;
[fileURL getResourceValue:&aBool forKey:NSURLIsHiddenKey error:nil];
if (aBool && ![aBool boolValue]) {
NSLog(@"Adding following Stack: %@", fileURL);
[_iCloudURLs addObject:fileURL];
}
}
NSLog(@"Found %d iCloud files", _iCloudURLs.count);
NSLog(@"iCloud URLs: %@", _iCloudURLs);
...
}