Should I use the FileManager or NSMetadataQuery to retrieve filenames in iCloud, by an IOS/Swift app?

170 Views Asked by At

I see people are using the FileManager and NSMetadataQuery to retrieve filenames in iCloud, when programming an IOS/Swift app. Which is better, and safer ? It seems Apple wants you to use NSMetadataQuery, but the advantage for the FileManager, is that the code will also work on the IOS device as well as iCloud, and also provides file attributes in addition to filenames.

For example using FileManager it can be done this way:

let contents =
    try FileManager.default.contentsOfDirectory(at: directoryURL,
                                                includingPropertiesForKeys: nil,
                                                options: [.skipsHiddenFiles])
for file in contents {
    …
}

Using NSMetadataQuery like this:

self.nsMetadataQuery!.predicate = NSPredicate(format: "%K LIKE '*'",
                //                                              argumentArray: [NSMetadataItemPathKey, path, NSMetadataItemFSNameKey])
1

There are 1 best solutions below

0
On

It seems, that Apple Docs encourage you to use NSMetadataQuery for searching documents in iCloud Container instead of File Manager API.

So probably NSMetaDataQuery is the way to go.