Files
Files copied to clipboard
iterating over subFolders in background
Hi I have next code:
guard
let url = self.documentDirURL,
let folder = try? Folder(path:url.path)
else { return }
DispatchQueue.global().async {
folder.makeSubfolderSequence(recursive: true).forEach { folder in
let list = folder.subfolders.filter({
$0.extension == self.projectExtention && $0.nameExcludingExtension != self.excludeProject
}).flatMap { $0.parent }
urls += list
}
}
as you may see - it scans all subfolders for subfolders with specific extension excluding specific filename, and put their parent folders in a list.
But when I put it in a DispatchQueue.global().async {} block (as shown above) - it stops at
while let parentReferenceRange = path.range(of: "../") {
line in method
func pathByFillingInParentReferences(for path: String, prependCurrentFolderPath: Bool = false) throws -> String {
with error: Thread 2: EXC_BAD_ACCESS (code=2, address=0x70000bf20ff0)
In a stack I see over 1000 calls of FileSystemIterator.next()
, so I think it's a recursion.
What I did wrong and how to fix it?
I seem to have the same problem.
After all, I rebuilt it using FileManager class...
It's not only can work async, but also 5 times faster
let url = PARRENT_DIR
let disk = FileManager.default
let queue = OperationQueue()
queue.addOperation {
guard let files = try? disk.subpathsOfDirectory(atPath: url.path) else {return}
let list = files
.filter({.....})
.map({ url.appendingPathComponent($0).path })
}
queue.waitUntilAllOperationsAreFinished()
print(list.count)
Yep same problem
I guess file manager is the only way to do it?