Skip to content

Instantly share code, notes, and snippets.

@yakushevichsv
yakushevichsv / DispatchOnMainAsync_Excerpt.swift
Last active June 21, 2021 18:40
Dispatch execution to main thread from callback
receiveItems { //apparantly the method's declaration is that: receiveItems(completion: ()-> Void)
//do some processing in completion block
let items = ...
DispatchQueue.main.async { [weak self] in
// update ui
guard let self = self else { return }
self.items = items
}
}
@yakushevichsv
yakushevichsv / MainThreadRunnerType.swift
Created June 21, 2021 18:55
MainThreadRunnerType.runOnMain
import UIKit
// MARK: - MainThreadRunnerType
protocol MainThreadRunnerType {
func runOnMain(_ block: @escaping () -> Void) // if not on main thread then run block asynchronously
func runOnMainWeakly<Object: AnyObject, T>(_ obj: Object,
parameter: T,
method: @escaping ((Object) -> (T) -> Void))
func runOnMainWeakly<Object: AnyObject>(_ obj: Object,
method: @escaping ((Object) -> () -> Void))
@yakushevichsv
yakushevichsv / MainThreadRunnerType+Weakify.swift
Created June 21, 2021 19:05
Extension of MainThreadRunnerType For Weakly Calling Block
extension MainThreadRunnerType {
static func weakify<Object: AnyObject, Input>(_ obj: Object,
method: @escaping ((Object) -> (Input) -> Void)) -> ((Input) -> Void) {
return { [weak obj] value in
guard let obj = obj else { return }
method(obj)(value)
}
}
static func weakify<Object: AnyObject>(_ obj: Object,
@yakushevichsv
yakushevichsv / MainThreadRunnerType+Sample.swift
Last active June 21, 2021 19:19
Contains sample of MainThreadRunnerType's usage
class Test1: MainThreadRunnerType {
func displayMessage(_ text: String) {
debugPrint(text)
}
func displayMessage(optText: String?) {
guard let text = optText else { return }
displayMessage(text)
}
@yakushevichsv
yakushevichsv / UILabel+Multiline.swift
Created July 31, 2021 09:23
Kind of vertical stack view & Extensions
import UIView
extension UILabel {
func markAsMultiline() {
numberOfLines = 0
lineBreakMode = .byWordWrapping
}
}
@yakushevichsv
yakushevichsv / DispatchQueue_getSpecific_test.swift
Created August 29, 2021 16:14
Test code for checking DispatchQueue.getSpecific method
final class Test {
private let queue: DispatchQueue
private let queueKey : DispatchSpecificKey<Int>
private let queueKeyValue: Int
init() {
let queueKey = DispatchSpecificKey<Int>()
let queueKeyValue = Int(arc4random())
self.queueKey = queueKey
@yakushevichsv
yakushevichsv / UnfairLock.swift
Created September 5, 2021 08:53
os_unfair_lock_t instead of NS{Recursive}Lock
// MARK: - UnfairLock
final class UnfairLock {
private let unfairLock: os_unfair_lock_t //UnsafeMutablePointer<os_unfair_lock>
private let unfairValue: os_unfair_lock_s
init() {
unfairLock = .allocate(capacity: 1)
unfairValue = .init()
unfairLock.initialize(to: unfairValue)
}
@yakushevichsv
yakushevichsv / AtomicPropertyWrapper.swift
Created September 5, 2021 09:41
Atomic Property Wrapper Using Unfair Lock
// MARK: - Atomic
@propertyWrapper struct Atomic<T> {
private let lock = UnfairLock()
private var value: T
var wrappedValue: T {
get {
lock.lock()
defer {
@yakushevichsv
yakushevichsv / Optional+Ext.swift
Created September 22, 2021 18:31
Contains extensions on Optional type (DSL wrappers)
import Foundation
extension Optional {
var hasValue: Bool {
if case .some = self {
return true
}
return false
}
@yakushevichsv
yakushevichsv / String+Empty.swift
Created September 22, 2021 19:23
Empty String
extension String {
static let empty = "" // swift 5.5 allows static let constructs in extension
#if DEBUG
func check() {
assert(Self.empty == "")
}
#endif
}