JohnSundell / JohnSundell/Files

iterating over subFolders in background

Open
#42 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Swift
Stars
2.6k
Forks
197
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with pathByFillingInParentReferences(for:prependCurrentFolderPath:) and FileSystemIterator.next(), then reproduce the recursive subfolder scan inside DispatchQueue.global().async. Inspect the stack around the ../ path handling and iterator traversal; done means the scan completes without EXC_BAD_ACCESS and the reported case is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.