Multithreading in iOS-Part 4/4

How can we cancel task in GCD?

Why do we need to avoid excessive thread creation?

  1. Too many blocking task are added to concurrent queues forcing the system to create additional threads until system run out of threads for your app.
  2. Too many private concurrent dispatch queues exist that all consumes thread resource.

What happens when you block main thread?

What is DispatchQueue.main and DispatchQueue.global ?

Why we need to update UI in main thread?

  • Assume you can change view’s properties asynchronously, shall these changes become effective at the same time?
  • If a UITableView remove a cell on a background thread, then another background thread operate this cell’s index, it may causes crash
  • If a background thread remove a view, and this thread’s RunLoop is not over, at the same time the user tap this removed view then what happens should it respond to the touch event? which thread need to respond?
  • UIKit: Contains all kinds of components, handles user events, it does not contain any rendering code.
  • Rendering Server: Responsible for drawing and displaying the view. which is a part in core animation.
  • Core Animation: Responsible for drawing, displaying and animating all views.
  • Commit Transaction: It handles layout of views, image decoding and format conversion operations and send to Render Server.
  • Render Server: Rendering, analyse the package sent from Commit Transaction and deserialisation into a rendering tree. Then it will generate drawing instructions by view layer’s properties, and call OpenGL to render screen when the next VSync Signal comes.
  • OpenGL ES: Provide 2D and 3D rendering server.
  • Core Graphics: Provide 2D rendering server.
  • GPU: GPU will wait for screen’s VSync Signal, then use OpenGL rendering pipeline to render. After rendering the output will send to buffer i.e frame buffer.
  • Display: Get data from buffer, and send to screen to display.

Why UIKit is not thread safe?

What happens when we Dispatching on the same queue?

What is Producer-Consumer Problem or race condition?

  1. Semaphore(DispatchSemaphore) — allows up to N threads to access a given region of code at a time.
  2. Mutex — ensures that only one thread is active in a given region of code at a time. You can think of it as a semaphore with a maximum count of 1 or pthread_mutex_t.
  3. Spinlock(OSSpinLock) — causes a thread trying to acquire a lock to wait in a loop while checking if the lock is available. It is efficient if waiting is rare, but wasteful if waiting is common.
  4. Read-write lock (pthread_rwlock_t)— provides concurrent access for read-only operations, but exclusive access for write operations. Efficient when reading is common and writing is rare.
  5. Recursive lock — a mutex that can be acquired by the same thread many times.

Priority Inversion:

Run Loop and View Drawing Cycle

  • In a Cocoa application, use the currentRunLoop class method of NSRunLoop to retrieve an NSRunLoop object.
  • Use the CFRunLoopGetCurrent function
1. get current runloop and 2. Get main runloop
  1. Configure the run loop to run with a timeout value. :- Specify a timeout value lets the run loop finish all of its normal processing, including delivering notifications to run loop observers, before exiting.
  2. Tell the run loop to stop:- stopping explicitly using CFRunLoopStop function. This also produce same result as timeout. The run loop sends out any remaining run-loop notifications and then exits. The difference is that you can use this technique on run loops that you started unconditionally.
  • Default:- Most of the time, you should use this mode to start your run loop and configure your input sources.
  • Connection:- Cocoa uses this mode in conjunction with NSConnection objects to monitor replies.
  • Modal:- Cocoa uses this mode to identify events intended for modal panels.
  • Event tracking:- uses this mode to restrict incoming events during mouse-dragging loops and other sorts of user interface tracking loops.
  • Common modes:- This is a configurable group of commonly used modes. For Cocoa applications, this set includes the default, modal, and event tracking modes by default. Core Foundation includes just the default mode initially. You can add custom modes to the set using the CFRunLoopAddCommonMode function.
  1. Input sources deliver:- It is a asynchronous events, usually messages from another thread or from a different application or mouse click or any user interaction
  2. Timer sources deliver:- Synchronous events, occurring at a scheduled time or repeating interval. Both types of source use an application-specific handler routine to process the event when it arrives.
  1. Port-Based Sources
  2. Custom Input Sources
create timer in current run loop
create a runloop within the thread
  • The entrance to the run loop.
  • When the run loop is about to process a timer.
  • When the run loop is about to process an input source.
  • When the run loop is about to go to sleep.
  • When the run loop has woken up, but before it has processed the event that woke it up.
  • The exit from the run loop.
  1. Notify observers that the run loop has been entered.
  2. Notify observers that any ready timers are about to fire.
  3. Notify observers that any input sources that are not port based are about to fire.
  4. Fire any non-port-based input sources that are ready to fire.
  5. If a port-based input source is ready and waiting to fire, process the event immediately. Go to step 9.
  6. Notify observers that the thread is about to sleep.
  7. Put the thread to sleep until one of the following events occurs:
  • An event arrives for a port-based input source.
  • A timer fires.
  • The timeout value set for the run loop expires.
  • The run loop is explicitly woken up.
  • If a user-defined timer fired, process the timer event and restart the loop. Go to step 2.
  • If an input source fired, deliver the event.
  • If the run loop was explicitly woken up but has not yet timed out, restart the loop. Go to step 2.

--

--

--

iOS Developer in walmart

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Come and Let’s PLAY DUNK IT NOW! #WSGTOKEN

Shower Animation SwiftUI

Create List & Navigation using SwiftUI

2 iPhone displaying a SwiftUI list and a details view

Flutter Deep links: iOS (Universal links) and Android (App links)

iOS 14: Is It Worth Updating

Classes and Structs

Is It Possible to Use SwiftUI Preview on UIViewController?

Error Unable To Restore Idevice 26 3utools

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Manasa M P

Manasa M P

iOS Developer in walmart

More from Medium

Annotated Bibliography assessment brief

iOS Jailbreak | AltDeploy | G0blin

12 Principles Of Animation — Part 9 — Timing

Filter Designing (Butterworth)