Skip to content

Instantly share code, notes, and snippets.

Avatar

Natan Rolnik natanrolnik

View GitHub Profile
@natanrolnik
natanrolnik / Redundant Else
Created Dec 18, 2019
Examples of redundant else usage
View Redundant Else
------ ⭣ Redundant Else ⭣ ------
if someCondition {
return meh
} else {
let someValue = callSomeFunction()
return someValue
}
----------- ⭣ Fine ⭣ -----------
View CodingKeys.swift
//declare which keys in the JSON we are interested in
enum CodingKeys: String, CodingKey {
case status
case confirmedUsers
case position
case reason
}
//declare the possible values os the status key
private enum EventConfirmationStatus: String, Codable {
View CoreData+Extensions.swift
//The following code depends on this:
//https://gist.github.com/sisoje/f1444dff45618938ce81324a81316690#file-nspredicate-keypath-operators-swift
import CoreData
extension NSManagedObject {
static var entityName: String {
return entity().name!
}
}
@natanrolnik
natanrolnik / Dispatcher.swift
Created Feb 12, 2019
A small wrapper that manages multiple DispatchWorkItems
View Dispatcher.swift
import Foundation
class Dispatcher {
private var items = [DispatcherIdentifier: DispatchWorkItem]()
private let queue: DispatchQueue
deinit {
cancelAllActions()
}
View CancelDispatchWorkItem.swift
var micHintWorkItem: DispatchWorkItem?
func recordVoiceMessage() {
micHintWorkItem?.cancel()
}
//in your viewDidAppear method, create and schedule the work item:
micHintWorkItem = DispatchWorkItem { [weak self] in
self?.micButton.jump()
}
View CancelAsyncAfter.swift
//keep a variable to know if the user tapped the button:
var micButtonTapped = false
func recordVoiceMessage() {
//if the user tapped/held the mic button, set the variable to true
micButtonTapped = true
}
//in your viewDidAppear method, schedule it:
DispatchQueue.main.asyncAfter(.now() + 3) { [weak self] in
View SimpleAsyncAfter.swift
//in your viewDidAppear method, schedule it:
DispatchQueue.main.asyncAfter(.now() + 3) { [weak self] in
self?.micButton.jump()
}
@natanrolnik
natanrolnik / NotificationThrottler.swift
Created Feb 12, 2019
A simple notification throttler using DispatchWorkItem
View NotificationThrottler.swift
class NotificationThrottler {
let notificationCenter: NotificationCenter
let timeInterval: TimeInterval
let handler: () -> Void
private var workItem: DispatchWorkItem?
deinit {
notificationCenter.removeObserver(self)
}
@natanrolnik
natanrolnik / Scheduler2.swift
Created Feb 8, 2019
Another small wrapper for multiple DispatchWorkItems
View Scheduler2.swift
PlaygroundPage.current.needsIndefiniteExecution = true
typealias DispatcherIdentifier = String
extension DispatchQueue {
static var associatedValueKey = 0
func schedule(after timeInterval: TimeInterval,
with identifier: DispatcherIdentifier,
action: @escaping () -> Void) {
@natanrolnik
natanrolnik / SchedulerPlayground.swift
Last active Nov 19, 2019
A playground for a small wrapper that manages multiple DispatchWorkItems
View SchedulerPlayground.swift
import Foundation
import PlaygroundSupport
typealias DispatcherIdentifier = String
class Dispatcher {
private var items = [DispatcherIdentifier: DispatchWorkItem]()
private let queue: DispatchQueue
You can’t perform that action at this time.