How to control the local document directory NSMetaDataQuery

With iCloud, I configured MetaDataQuery like this:

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

How to configure the same MetaDataQuery for my regular local document directory?

The following code will provide me with a static list of files in my directory, but I was looking for a more dynamic way to use NSMetadataQueryDidUpdateNotification .

Here is my static code for finding files in my documents. Directory:

 NSArray* localDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: [self documentsDirectory] error:nil]; NSArray *onlyPQs = [localDocuments filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.pq'"]]; NSLog(@"local file count: %i", [onlyPQs count]); for (int i=0; i < [onlyPQs count]; i++) { NSLog(@"LOCAL:%@", [onlyPQs objectAtIndex:i]); } 
+5
source share
2 answers

I was looking for the answer to this question and, according to the documentation ( iCloud Search and desktop ), this is not possible:

Unlike the desktop, the sandbox for iOS applications is not searchable using metadata classes. To search for your sandbox applications, you will need to go through the files inside the sandbox file. The system recursively uses the NSFileManager class.

Mac OS X defines three additional areas: NSMetadataQueryUserHomeScope , NSMetadataQueryLocalComputerScope and NSMetadataQueryNetworkScope that can be used to achieve this.

+6
source

Match the NSFilePresenter class and use the instance with [NSFileCoordinator initWithFilePresenter:] .

Use an instance of NSFileCoordinator to coordinate reading the directory URL, and your subclass should receive updates through the following callbacks (not a complete list, there are others):

  • presentedSubitemDidChangeAtURL:
  • presentedSubitemDidAppearAtURL:
  • presentedSubitemAtURL:didMoveToURL:
  • accommodatePresentedSubitemDeletionAtURL:completionHandler:

Subelements are descendants of the directory that is being read. If the directory provided is itself a file package, some additional rules apply. Check out the NSFilePresenter .

Edit : some NSFilePresenter methods just don't get called due to some bugs on the Apple side. See fooobar.com/questions/1472032 / ... and https://www.objc.io/issues/10-syncing-data/icloud-document-store/#defective-notifications .

This method requires file system changes to be made using NSFilePresenter . Changes made in other ways are not recorded.

0
source

Source: https://habr.com/ru/post/1381809/


All Articles