Skip to content

swiftarium/AutoCleaner

Repository files navigation

AutoCleaner

AutoCleaner is a utility in Swift designed to automatically clean elements from a given collection. Based on a specified condition and frequency, it ensures your collection remains tidy and efficient.

Installation

To install AutoCleaner into your Xcode project using SPM, add it to the dependencies value of your Package.swift:

dependencies: [
    .package(url: "https://github.com/jinyongp/AutoCleaner.git", from: "1.2.0"),
]

And specify "AutoCleaner" as a dependency of the Target in which you wish to use AutoCleaner.

targets: [
    .target(name: "YourTarget", dependencies: ["AutoCleaner"]),
]

Usage

Initialization

Initialize AutoCleaner by providing a collection and a condition:

let cleaner = AutoCleaner([1, 2, 3, 4, 5]) { $0 <= 3 }

Start and Stop Cleaning

Start automatic cleaning with a specified frequency:

// count is the number of elements in the collection
cleaner.start { count in 
    if count > 100 {
        return .seconds(30)
    } else {
        return .seconds(60)
    }
}

To stop the cleaning:

cleaner.stop()

Accessing the collection

cleaner.items.forEach { print($0) }

Manually initiate a clean operation:

cleaner.clean()

Callbacks

Get notified after every cleaning operation:

cleaner.onCleaned = { removedCount in 
    print("\(removedCount) elements removed.")
}

License

This library is released under the MIT license. See LICENSE for details.

About

An automatic, self-adjusting cleaner for collections in Swift.

Resources

License

Stars

Watchers

Forks

Languages