Group Group Group Group Group Group Group Group Group

Swift: What is the best way to do Safe Thread Concurrent or NSLock

What is the best way to do Safe Thread?

NSLock or a Queue with .concurrent attribute for the following case, and why?

Using NSLock:

class Observable<T> {

	typealias Observer = (_ observable: Observable<T>, T) -> Void
	
	private var observers: [Observer]
	private let lock = NSLock()
	private var _value: T

	var value: T {
		get {
			lock.lock()
			let value = _value
			lock.unlock()
			return value
		}
		set {
			lock.lock()
			_value = newValue
			lock.unlock()
		}
	}

	
	init(_ value: T) {
		self._value = value
		observers = []
	}

	func observe(observer: @escaping Observer) {
		self.observers.append((observer))
	}

	private func notifyObservers(_ value: T) {
		DispatchQueue.main.async {
			self.observers.forEach { [unowned self](observer) in
				observer(self, value)
			}
		}
	}

}

Using Queue:

class SecondObservable<T> {

	typealias Observer = (_ observable: SecondObservable<T>, T) -> Void
	
	private var observers: [Observer]
	private let safeQueue = DispatchQueue(label: "com.observable.value", attributes: .concurrent)
	private var _value: T

	var value: T {
		get {
			var value: T!
			safeQueue.sync { value = _value }
			return value
		}
		set {
			safeQueue.async(flags: .barrier) { self._value = newValue }
		}
	}

	
	init(_ value: T) {
		self._value = value
		observers = []
	}

	func observe(observer: @escaping Observer) {
		self.observers.append((observer))
	}

	private func notifyObservers(_ value: T) {
		DispatchQueue.main.async {
			self.observers.forEach { [unowned self](observer) in
				observer(self, value)
			}
		}
	}

}