QoS

Note: iOS 8+ should use QoS instead of system default queues (times showed are just examples)
  • User-interactive: UI feedback. Such as animation. (<1s)
  • User-initiated: UI works that required in order to continue the interaction. Such as opening a file. (1~3s)
  • Utility: UI works that have no need for an immediate result. Such as downloading a file. (3s~3m)
  • BackgroundNon-UI works. Such as indexing, backup. (3m~3h)

Location tracking and battery usage

  • Visits location service (CLVisit): most power-efficient way. Not for navigation or real-time. Good for doing async background processing related to location. Or to identify location pattern. Requires Always authorization. 
  • Region monitoring (Geofencing): Define a circular region and your app gets a notification when crossing the boundary. Apple’s built-in Reminder app uses that too. Very little battery consumption. It is built upon Core Location’s significant-change location service. Requires Always authorization. 
  • Significant-change location service: power-friendly way. Requires Always authorization. 
  • Standard location service: Real-time. Most power consumption. 

Capture list/value

Notes from Capturing Values In Swift Closures

Within a closure, referring to variables outside of the closure, strong references are created.

class MyClass {
let a = 1
let myClosure = {
print(self.a)
}
myClosure() // 1
a = 2
myClosure() // 2
}

If we don’t want the strong ref, we can specify weak

class MyClass {
let a = 1
let myClosure = { [weak self] in
guard let strongSelf = self else { return }
print(strongSelf.a)
}
myClosure() // 1
a = 2
myClosure() // 1 <-- here's the difference
}

Dispatch Queues

What? A queue that runs tasks FIFO.

Types? 

  • Serial – one at a time
    • system-provided: main queue
    • you can create your own
  • Concurrent – 1+ tasks at a time
    • system-provided: 4 queues (high, default, low, and background)
    • you can create your own, but 4 system ones should be good enough
Note: iOS 8+ should use QoS instead.