Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterating over subFolders in background #42

Open
Briahas opened this issue Dec 13, 2017 · 3 comments
Open

iterating over subFolders in background #42

Briahas opened this issue Dec 13, 2017 · 3 comments

Comments

@Briahas
Copy link

Briahas commented Dec 13, 2017

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?

@SF-Simon
Copy link

I seem to have the same problem.

@Briahas
Copy link
Author

Briahas commented Jan 4, 2018

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)

@bryan1anderson
Copy link

Yep same problem

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants