Skip to content

Instantly share code, notes, and snippets.

View loganblevins's full-sized avatar

Logan Blevins loganblevins

View GitHub Profile
@funmia
funmia / todoapi.md
Created November 24, 2018 20:21
sample urlsession call using https://jsonplaceholder.typicode.com
struct Todo: Decodable {
    let userId: Int
    let id: Int
    let title: String
    let completed: Bool
}

func getToDoItems(completionHandler: @escaping ([Todo]) -> Void) {
@aainaj
aainaj / DispatchBarrier.swift
Last active April 3, 2023 18:02
Dispatch Barrier to solve race condition
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
var value: Int = 2
let concurrentQueue = DispatchQueue(label: "queue", attributes: .concurrent)
concurrentQueue.async(flags: .barrier) {
@lanserxt
lanserxt / LoopedPlayerView.swift
Last active October 25, 2023 16:18
Loop playback with AVQueuePlayer and AVPlayerLooper
final class LoopedVideoPlayerView: UIView {
fileprivate var videoURL: URL?
fileprivate var queuePlayer: AVQueuePlayer?
fileprivate var playerLayer: AVPlayerLayer?
fileprivate var playbackLooper: AVPlayerLooper?
func prepareVideo(_ videoURL: URL) {
let playerItem = AVPlayerItem(url: videoURL)
@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active July 8, 2025 03:48
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@BrianLitwin
BrianLitwin / FetchRequestGenerator.swift
Created January 26, 2018 04:46
A Generic fetch request protocol that uses an Associated Type to send fetch requests through an Enum
import CoreData
let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
class Book: NSManagedObject {
var title: String?
var author: Author?
var date: Date?
}
@skyebook
skyebook / CVPixelBufferPixelFormatNames.swift
Created March 24, 2017 15:26
Easily get the pixel format name of a CVPixelBuffer
public func CVPixelBufferGetPixelFormatName(pixelBuffer: CVPixelBuffer) -> String {
let p = CVPixelBufferGetPixelFormatType(pixelBuffer)
switch p {
case kCVPixelFormatType_1Monochrome: return "kCVPixelFormatType_1Monochrome"
case kCVPixelFormatType_2Indexed: return "kCVPixelFormatType_2Indexed"
case kCVPixelFormatType_4Indexed: return "kCVPixelFormatType_4Indexed"
case kCVPixelFormatType_8Indexed: return "kCVPixelFormatType_8Indexed"
case kCVPixelFormatType_1IndexedGray_WhiteIsZero: return "kCVPixelFormatType_1IndexedGray_WhiteIsZero"
case kCVPixelFormatType_2IndexedGray_WhiteIsZero: return "kCVPixelFormatType_2IndexedGray_WhiteIsZero"
case kCVPixelFormatType_4IndexedGray_WhiteIsZero: return "kCVPixelFormatType_4IndexedGray_WhiteIsZero"
@benbahrenburg
benbahrenburg / UIImageToDataTests.swift
Last active January 26, 2024 19:49
Swift Playground for testing memory associated with converting UIImage to Data
import UIKit
import ImageIO
import MobileCoreServices
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
func report_memory() -> UInt64 {
var taskInfo = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4