Mar 17, 2020

Structural Design Patterns in iOS

The wikipedia definition of a software design pattern is

A reusable solution to a commonly occuring problem in the context of software design.

Gang of four book in 1994 formalised a number of design patterns. Here we are going to talk more specifically about the common design patterns when it comes to iOS app development.

This is different from the architectural design patterns, MVC or MVVM or Viber or clean architecture.

Structural design patterns are more about managing the piece of codes in reusable way.

Let’s summarize them:

Read on  

  Mar 12, 2020

Dynamic vs Static

When someone says something is dynamic, it usually means things could change. Dynamic array, meaning the content of the array could change, or a dynamic system could mean a system that is robust and can adapt to different input scenario.

So, what does it mean for a framework to be a dynamic framework, or static framework? Or when someone says Python is a dynamically typed language but Swift is a statically typed language?

Let’s dive in.

Read on  

  Mar 26, 2019

Deadlock example

Deadlock is a situation when two or more threads are waiting on each other to be completed causing both to be locked and not able to continue.

A practical example of a deadlock is

Let’s see an example in swift.

here, the the queue is exeucting with q.async first, inside the block it calls for q.sync. So the outer block can only complete when the inner block q.sync is finished, but q.sync cannot start, because it is a sync block unless the thread is free. This results in a deadlock and the process will crash.

import Foundation

class Resource {
	
	private(set) var value: Int
	
	private let q = DispatchQueue(label: "myQueue")
	
	init(_ value: Int) {
		self.value = value
	}

	// Deadlock
	func set(_ value: Int) {
		q.async {
			print("async")
			self.q.sync {
				self.value = value
				print("sync") 	 	// outer block is waiting for this inner block to complete,
        							// inner block won't start before outer block finishes
        							// => deadlock
			}
			print("finished") // this will never be reached
		}
	}
}

let r = Resource(100)
r.set(200)

print("final value \(r.value)")

// Output
// async
// Execution Interrupted due to deadlock
Read on  

  Mar 26, 2019

Swift Race Condition: ATM Withdraw Problem

Race condition happens when 2 or more threads access the shared data at the same time and change its value at the same time resulting in unexpected state.

A simple example is the typical ATM withdraw problem.

ATM withdraw problem

Let’s say we have our own bank account system and we have ATMs depoloyed all over the town to make withdrawl easy.

Read on  

  Mar 19, 2019

Latest swift tips

It’s been a while I’ve updated my posts. Here are some new tips that makes coding with swift cool.

@autoclosure

@autoclosure attribute defines an argument that automatically gets wrapped in a closure. It’s used to defer the execution of an expression to the site of use, rather than when the argument is passed.

Example:

func evaluate(array: [String], emptyValue: @autoclosure () -> String) {
	if array.size > 0 {
		print("array size \(array.size)")
	} else {
		print(emptyValue())
	}
}

evaluate(["one", "two", "three"], "emptyArray") // array size 3
evaluate([], "blank") // blank

Difference between @escaping and nonescaping

Read on