Skip to content

A property wrapper that helps you access any type with thread safety

License

Notifications You must be signed in to change notification settings

iThink32/GenericThreadSafePropertyWrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

ThreadSafePropertyWrapper

Why is it needed?

Have you ever wanted something like @atomic in Swift? , have you ever faced the issue with race conditions in specific properties of your data models? This repo handles that , be it any type just use this and the accesses to that instance will be threadsafe

Why Shared CustomQueue?

  • Every application is given a limited number of threads and when you create multiple threads it will eventually lead to thread starvation and your application will enter a memory warning state.This is why i am using a shared concurrent queue.

Why barrier on ConcurrentQueue and not on Global queue?

  • barriers do not work on global concurrent queues , it works only on custom concurrent queues

Best Part?

It is generic , can be used over any type and is even backward compatible all the way even upto iOS 11 , give it a spin :)

Usage:

Consider the following model without threadsafety

public class BaseModel {
    public var investedAmount: Decimal
    public var currentInvestmentValue: Decimal
    public var currentReturns: Decimal
    public var currentReturnsPercentage: Decimal

    init(investedAmount: Decimal,
         currentInvestmentValue: Decimal,
         currentReturns: Decimal,
         currentReturnsPercentage: Decimal) {
        self.investedAmount = investedAmount
        self.currentInvestmentValue = currentInvestmentValue
        self.currentReturns = currentReturns
        self.currentReturnsPercentage = currentReturnsPercentage
    }
    
}

and with becomes

public class BaseModel {
    @ThreadSafe public var investedAmount: Decimal
    @ThreadSafe public var currentInvestmentValue: Decimal
    @ThreadSafe public var currentReturns: Decimal
    @ThreadSafe public var currentReturnsPercentage: Decimal

    init(investedAmount: Decimal,
         currentInvestmentValue: Decimal,
         currentReturns: Decimal,
         currentReturnsPercentage: Decimal) {
        self.investedAmount = investedAmount
        self.currentInvestmentValue = currentInvestmentValue
        self.currentReturns = currentReturns
        self.currentReturnsPercentage = currentReturnsPercentage
    }
    
}

Thats it! , your model is now thread safe :)

About

A property wrapper that helps you access any type with thread safety

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages