Files icon indicating copy to clipboard operation
Files copied to clipboard

iterating over subFolders in background

Open Briahas opened this issue 6 years ago • 3 comments

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?

Briahas avatar Dec 13 '17 19:12 Briahas

I seem to have the same problem.

SF-Simon avatar Dec 21 '17 06:12 SF-Simon

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)

Briahas avatar Jan 04 '18 13:01 Briahas

Yep same problem

I guess file manager is the only way to do it?

bryan1anderson avatar Jun 27 '18 14:06 bryan1anderson