Skip to content

Instantly share code, notes, and snippets.

@YogevSitton-zz
YogevSitton-zz / properties.m
Created April 26, 2017 06:40
atomic nonatomic properties
@property (atomic, strong) NSString *exampleAtomicProperty;
@property (nonatomic, strong) NSString *exampleNonAtomicProperty;
@YogevSitton-zz
YogevSitton-zz / reallySetProperty.m
Created April 24, 2017 12:37
reallySetProperty
static inline void reallySetProperty(id self, SEL _cmd, id newValue,
ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
{
id oldValue;
id *slot = (id*) ((char*)self + offset);
if (copy) {
newValue = [newValue copyWithZone:NULL];
} else if (mutableCopy) {
newValue = [newValue mutableCopyWithZone:NULL];
@YogevSitton-zz
YogevSitton-zz / DispatchQueuesCrash.swift
Created April 20, 2017 14:27
Beware of synchronously running on the main thread from a synchronous background thread
DispatchQueue.global(qos: .utility).sync {
// Background Task
DispatchQueue.main.sync {
// App will crash
}
}
@YogevSitton-zz
YogevSitton-zz / SerialQueuesDeadlock.swift
Created April 20, 2017 14:26
Beware of deadlocks with serial queues
let customSerialQueue = DispatchQueue(label: "com.yogevsitton.MyApp.myCustomSerialQueue")
customSerialQueue.sync {
// Synchronous code
customSerialQueue.sync {
// This code will never be executed and the app is now in deadlock
}
}
@YogevSitton-zz
YogevSitton-zz / BackgroundToMain.swift
Created April 20, 2017 14:24
Performing background tasks and then updating the UI
DispatchQueue.global(qos: .background).async {
// Do some background work
DispatchQueue.main.async {
// Update the UI to indicate the work has been completed
}
}
@YogevSitton-zz
YogevSitton-zz / SystemDispatchQueues.swift
Created April 20, 2017 14:21
System Dispatch Queues
DispatchQueue.global(qos: .utility).async {
// Asynchronous code running on the low priority queue
}
DispatchQueue.main.async {
// Asynchronous code running on the main queue
}
DispatchQueue.global(qos: .userInitiated).sync {
// Synchronous code running on the high prioriy queue
@YogevSitton-zz
YogevSitton-zz / CustomDispatchQueues.swift
Last active April 20, 2017 14:18
Custom Dispatch Queues
let customSerialQueue = DispatchQueue(label: "com.yogevsitton.MyApp.myCustomSerialQueue")
customSerialQueue.async {
// Code
}
let customConcurrentQueue = DispatchQueue(label: "com.yogevsitton.MyApp.myCustomConcurrentQueue", attributes: .concurrent)
customConcurrentQueue.async {
// Code
}
@YogevSitton-zz
YogevSitton-zz / AutoDescribingObjects.swift
Last active March 9, 2022 10:05
Use auto-describing objects with CustomStringConvertible
extension CustomStringConvertible {
var description : String {
var description: String = ""
if self is AnyObject {
description = "***** \(self.dynamicType) - <\(unsafeAddressOf((self as! AnyObject)))>***** \n"
} else {
description = "***** \(self.dynamicType) *****\n"
}
let selfMirror = Mirror(reflecting: self)
for child in selfMirror.children {