Swiftpack.co - hainayanda/Chary as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by hainayanda.
hainayanda/Chary 1.0.1
Thread Utility library
⭐️ 1
🕓 3 weeks ago
iOS macOS tvOS
.package(url: "https://github.com/hainayanda/Chary.git", from: "1.0.1")


Chary is a DispatchQueue Utilities for safer sync and asynchronous programming. It helps to avoid a race condition when dealing with multithreaded application

codebeat badge build test SwiftPM Compatible Version License Platform


  • Swift 5.0 or higher (or 5.3 when using Swift Package Manager)
  • iOS 10.0 or higher

Only Swift Package Manager

  • macOS 10.0 or higher
  • tvOS 10.10 or higher



Chary is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Chary'

Swift Package Manager from XCode

  • Add it using XCode menu File > Swift Package > Add Package Dependency
  • Add https://github.com/hainayanda/Chary.git as Swift Package URL
  • Set rules at version, with Up to Next Major option and put 1.0.1 as its version
  • Click next and wait

Swift Package Manager from Package.swift

Add as your target dependency in Package.swift

dependencies: [
  .package(url: "https://github.com/hainayanda/Chary.git", .upToNextMajor(from: "1.0.1"))

Use it in your target as Chary

    name: "MyModule",
    dependencies: ["Chary"]


Nayanda Haberty, hainayanda@outlook.com


Pharos is available under the MIT license. See the LICENSE file for more info.

Basic Usage

Two utilities come with Chary, Atomic propertyWrapper and DispatchQueue extensions

Atomic propertyWrapper

Atomic propertyWrapper is a propertyWrapper to wrap a property so it could be accessed and edited atomically:

class MyClass {
    @Atomic var atomicString: String = "atomicString"

then the atomicString will be Thread safe regardless of where it is accessed or edited.

DispatchQueue.main.async {
    myClass.atomicString = "from main thread"
DispatchQueue.global().async {
    myClass.atomicString = "from global thread"

DispatchQueue Extensions

Chary has some DispatchQueue Extension that will help when dealing with multithreaded.

Registering DispatchQueue detection

You can register DispatchQueue for detection so it will be available for check:

myQueue = DispatchQueue(label: "myQueue")

So later you can check the current DispatchQueue like this:

let isRunInMyQueue = DispatchQueue.current == myQueue

Calling current will automatically register all system DispatchQueue like main or global

Safe Sync

Running sync from DispatchQueue sometimes can raise an exception if it is called in the same DispatchQueue. To avoid this, you can use safeSync instead which will check the current queue first and decide whether it needs to run the block right away or by using the default sync. You don't need to register the DispatchQueue since it will automatically register the DispatchQueue before checking:

DispatchQueue.main.safeSync {
    print("this will safely executed")

Async if needed

Sometimes you want to execute the operation right away if it's in the right DispatchQueue instead of running it asynchronously by using async. Like when you update UI, it's better if you run it right away instead of putting it in the asynchronous queue if you are already in DispatchQueue.main. You can use asyncIfNeeded to achieve that functionality right away. It will check the current DispatchQueue and decide whether it needs to run right away or by using the default async. You don't need to register the DispatchQueue since it will automatically register the DispatchQueue before checking:

DispatchQueue.main.asyncIfNeeded {
    print("this will executed right away or asynchronously if in different queue")


You know-how. Just clone and do a pull request


Stars: 1
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.


Related Packages

Release Notes

3 weeks ago
  • Make it available on iOS 10, macOS 10.10, tvOS 10
  • Make Atomic to be final

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics