iOS Life

  • Home
  • ABOUT
  • SERVICES
  • PORTFOLIO
  • BLOG
  • Contact

Tutorial: Threads

Threads allows you to execute multiple code paths concurrently within your application. The reason you’ll use it is to speed up the performance of your application by running some code on another thread while other operations are happening on the main thread. This is helpful for when you need to pre-load data. I’ve used this before in a magazine app where I needed to pre-load the next couple of pages while the user is reading the current page, this made the application much smoother when swiping to the next page.

There are a couple of disadvantages to threads, one problem with creating threads yourself is that they add uncertainty to your code. Threads are a relatively low-level and complicated way to support concurrency in your application. If you do not fully understand the implications of your design choices, you could easily encounter synchronization or timing issues, the severity of which can range from subtle behavioral changes to the crashing of your application and the corruption of the user’s data. Another problem area with Cocoa and multithreading comes from user interface updates. All UI updates in Cocoa must be done on the main thread, or instability can result. If you have a background thread performing a calculation, make sure to wrap whatever method updates the UI in a -performSelectorOnMainThread:withObject:waitUntilDone: method call. The single biggest source of crashes with multithreaded Cocoa applications is simultaneous access to a shared resource.

Grand Central Dispatch (GCD)

Grand Central Dispatch is one of the most common ways to add threads. With GCD, you define the task you want to perform and add it to a work queue, which handles the scheduling of your task on an appropriate thread. Work queues take into account the number of available cores and the current load to execute your tasks more efficiently than you could do yourself using threads.

Adding a Task to a Queue

dispatch_async(myQueue, ^{
    // Insert code to be executed on another thread here
    [self methodName];
});
dispatch_release(myQueue);

To execute a task, you must dispatch it to an appropriate dispatch queue. You can dispatch tasks synchronously or asynchronously. Once in a queue, the queue becomes responsible for executing your tasks as soon as possible, given its constraints and the existing tasks already in the queue. Do not call the dispatch_sync function from a task that is executing on the same queue that you pass to your function call. Doing so will deadlock the queue. If you need to dispatch to the current queue, do so asynchronously using the dispatch_async function.

Performing Tasks on the Main Thread

You can use to execute tasks on your application’s main thread while you’re running code on another thread. You can get the dispatch queue for your application’s main thread by calling the dispatch_get_main_queue function. Tasks added to this queue are performed serially on the main thread itself. Therefore, you can use this queue as a synchronization point for work being done in other parts of your application.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    // Insert code to be executed on another thread here
    [self methodName];
    dispatch_async(dispatch_get_main_queue(), ^{
        // Insert code to be executed on the main thread here
        [self methodName];
    });
});
dispatch_release(queue);

Suspending and Resuming Queues

It’s not possible to stop a queue after it has been called because it would cause your application to be unstable but you can pause and resume the queue. You suspend a dispatch queue using the dispatch_suspend function and resume it using the dispatch_resume function.

-(void) startRunning {
    // queue will need to be defined in your header since it's use globally in the class
    queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        // Insert code to be executed on another thread here
        [self methodName];
    });
}

-(void) userCancels {
    if (queue) {
       dispatch_suspend(queue);
    }
}

-(void) userResumes {
    if (queue) {
       dispatch_resume(queue);
    }
}

-(void) dealloc {
    if (queue) dispatch_release(queue);

    [super dealloc[;
}

Mar 27, 2013 Brian Coleman
Game Strategy: Keep Users Coming BackTutorial: Collection View using Flow Layout
You Might Also Like
 
Tutorial: 3D Touch – Quick Actions in Swift
 
Tutorial: Today Widget in Swift
7 years ago Tutorialsios, objective-c, tutorial13,011
Follow Me
    
Categories
  • About Me
  • Frameworks
  • My Apps
  • News
  • Strategy
  • Swift
  • Tools
  • Tutorials
  • tvOS
  • Uncategorized
  • Videos
  • Watch
Archives
  • May 2016
  • January 2016
  • October 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • November 2014
  • October 2014
  • September 2014
  • July 2014
  • June 2014
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
brianjcoleman on Twitter
  • Classix is still holding in the top charts on Apple TV in Canada. #55 Free, #23 Top Grossing. #tvos #appletv #app https://t.co/xuEJiT4rro, Jul 14
  • New Blog Post: "Classix for iPhone, iPad & Apple TV” #iOSDev #ios #swift #swiftlang #SwiftDevs #AppleTV Read here: https://t.co/uF6w3gYOot, May 20
  • New Blog Post: "How to test your app for IPv6 compatibility” #iOSDev #ios #swift #swiftlang #SwiftDevs Read here: https://t.co/SveichSUep, May 6

Recent Posts
  • Classix for iPhone, iPad & Apple TV
  • Tutorial: How to test your app for IPv6 compatibility
  • Tutorial: Testing SSL using Charles Proxy on an iOS Device
  • Tutorial: 3D Touch – Quick Actions in Swift
  • tvOS Tutorial: Top Shelf in Swift
Featured Apps
Classix
Sportsnet
TAGS
tutorialswiftios8iosobjective-cvideostrategygamesframeworknewsappsmonitizeios7applefacebookwatchtoolstvosios9bookdesignsocialapiprovisiontutorialsbooksiapiTunes ConnectIPv6
Search
ABOUT
Brian is a Lead iOS/tvOS Developer from Toronto with over 18 years of multifaceted experience including development, design, business analysis and project management.
MOST VIEWED
Tutorial: How To Use Login in Facebook SDK 4.1.x for Swift
162,608 views
Tutorial: How to test your app for IPv6 compatibility
99,880 views
Tutorial: How to use Auto Layout in Xcode 6
88,713 views
FOLLOW ME
    
Email Subscription
Sign up for my newsletter to receive the latest news and tutorials posted.

Enter your email address:

2013-2017 © Brian Coleman