Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Threading Safety First

DZone's Guide to

Threading Safety First

Have you ever wondered how to write a singleton in Swift?

· Mobile Zone
Free Resource

Now this is a truly epic investigation of the evolution of how to write a singleton in Swift:

THE RIGHT WAY A.K.A. “THE ONE LINE SINGLETON (NOW WITH PROOF!”)

 

class TheOneAndOnlyKraken {
    static let sharedInstance = TheOneAndOnlyKraken()
}



We knew about this “right” method of writing singletons, but we had no proof to back up our reasoning other than postulation … So I decided to do something about it and wrote up every way of initializing a singleton and inspected them at runtime using breakpoints. After analyzing each stack trace for any similarities I came across something interesting – PROOF!

*mic drop*

There’s been a number of other good posts lately in the area of threading and memory safety; we recommend checking out

Atomics in Objective-C

This post talks about the use of OS low level atomic functions (compare-and-swap, fetch-and-increment) to achieve both wait and lock free, thread-safe concurrent algorithms in your iOS/OSX apps.

It introduces a few classes that abstract away the gory details of some of the atomic functions made available by libkern/OSAtomic.h into conceptually simpler components that use Foundation types…

Strong, Weak, and Unowned – Sorting out ARC and Swift

I wanted to stop being unsure about the implications of typing one of those three words before variable / constant declarations, so I finally pulled up the Apple Documentation on ARC and began trying to digest the semantics. This article is an attempt to share what got sorted out in my brain as it regards ARC and Swift…

“Weak, Strong, Unowned, Oh My!” – a Guide to References in Swift

I often find myself worrying about retain cycles in my code. I feel like this is a common concern amongst others as well. I don’t know about you, but it seems like I am constantly hearing “When am I supposed to use weak? And what the hell is this ‘unowned’ crap?!” The issue we find is that we know to use strong, weak, and unowned specifiers in our swift code to avoid retain cycles, but we don’t quite know which specifier to use. Fortunately, I happen to know what they are AND when to use them!

Demystifying Retain Cycles in ARC

Retain cycles in ARC are kind of like a Japanese B-horror movie. When you start as a Cocoa/Cocoa Touch developer, you don’t even bother about their existence. Then one day one of your apps starts crashing because of memory leaks and suddenly you become aware of them, and start seeing retain cycles like ghosts everywhere. As the years pass, you learn to live with them, detect them and avoid them… but the final scare is still there, looking for its chance to creep in.

Tip: Avoid retain cycles without doing the strong to weak dance by passing block parameters instead of remembering to capture weakly.

And Grand Central Dispatch helper frameworks in Swift seem to be quite a thing lately; here’s some for you to check out:

Async: “Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch.”

Eki: “is a framework to manage easily concurrency in your apps that wraps the powerful API Grand Central Dispatch”

GCDKit: “Grand Central Dispatch simplified with Swift.”

iDispatch: “Easy to use wrapper over GCD for iOS.”

KillerRabbit: “THGDispatch module, includes GCD bits such as Queues, Groups, Timer, Semaphore, etc.”

Topics:
swift ,patterns

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}