saoudrizwan / Disk

Delightful framework for iOS to easily persist structs, images, and data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No performant way of checking if folder is empty

ullstrm opened this issue · comments

Hi!

I have a use case where I would like to check wether a folder is empty or not.

As of today, I use this:

    var hasContent: Bool {
        guard let data = try? Disk.retrieve(folder.name, from: directory, as: [Data].self) else { return false }
        return data.count > 0
    }

However this does not seem very performant. Is there possibility for a simple "folderIsEmpty" or such? We are storing data in a subfolder within the directory.

Maybe I'm missing something. And also, I'm not sure wether this should be posted as an issue at all. :)

This would be a great addition to Disk, something like Disk.isEmpty(directory) or Disk.paths(for: directory), although there is a blurry line between Disk being a simple persistence layer versus being a wrapper for FileManager.
For now you can do something like:

let path = try Disk.url(for: folder.name, in: .documents).path
let contents = try FileManager.default.contentsOfDirectory(atPath: path)
return contents.count > 0

This way, instead of loading all the data into memory to get a count, you just load the paths to your files using contentsOfDirectory(atPath: path).
Feel free to create a pull request if you want this to be part of Disk.

Thank you for the super quick answer! I understand the "blurry line" argument!