https://github.com/giannicarlo/directorywatcher
LIstener for changes in a specified folder
https://github.com/giannicarlo/directorywatcher
gcd ios listener swift watcher
Last synced: 5 months ago
JSON representation
LIstener for changes in a specified folder
- Host: GitHub
- URL: https://github.com/giannicarlo/directorywatcher
- Owner: GianniCarlo
- License: mit
- Created: 2018-07-20T03:51:22.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-06-20T18:26:51.000Z (over 4 years ago)
- Last Synced: 2025-05-01T12:51:44.029Z (5 months ago)
- Topics: gcd, ios, listener, swift, watcher
- Language: Swift
- Size: 38.1 KB
- Stars: 50
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
This is a Swift translation of the [Objective C version](https://github.com/hwaxxer/MHWDirectoryWatcher). Took some inspiration from [this repo](https://github.com/dagostini/DAFileMonitor/tree/blog_dispatch_sources) as well
# DirectoryWatcher
`DirectoryWatcher` is a lightweight class that uses GCD to monitor a given path for changes.
When any change to the directory occurs, `DirectoryWatcher` starts polling the monitored path, making sure that file transfers are finished before posting notifications.## Installing
### [CocoaPods](https://cocoapods.org/) (recommended)
````ruby
# For latest release in cocoapods
pod 'DirectoryWatcher'
````### Carthage
[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with [Homebrew](https://brew.sh/) using the following command:
```bash
$ brew update
$ brew install carthage
```To integrate DirectoryWatcher into your Xcode project using Carthage, specify it in your `Cartfile`:
```ogdl
github "GianniCarlo/DirectoryWatcher" ~> 2.0.0
```Run `carthage update` to build the framework and drag the built `DirectoryWatcher.framework` into your Xcode project.
### Swift Package Manager
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.
Once you have your Swift package set up, adding DirectoryWatcher as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.
```swift
dependencies: [
.package(url: "https://github.com/GianniCarlo/DirectoryWatcher.git", .upToNextMajor(from: "2.7.0"))
]
```## Usage (DirectoryWatcher)
Monitor the Documents Folder
```swift
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let watcher = DirectoryWatcher.watch(documentsUrl)watcher.onNewFiles = { newFiles in
// Files have been added
}watcher.onDeletedFiles = { deletedFiles in
// Files have been deleted
}
```
Call `watcher.stopWatching()` and `watcher.startWatching()` to pause / resume.## Usage (DirectoryDeepWatcher)
Monitor the Documents Folder and its subfolders
```swift
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let watcher = DirectoryDeepWatcher.watch(documentsUrl)watcher.onFolderNotification = { folder in
// New changes have happened inside one folder
// This folder could be a subfolder inside the root folder being watched
}```
Call `watcher.stopWatching()` and `watcher.startWatching()` to pause / resume, or `watcher.restartWatching()` to discard previous listeners and place new ones in case the hierarchy has changed