now i am trying to understand the concept of gcd. using grand central dispatch how to implement multithreading in my application.i have the idea about the gcd concept but i cant implement the concept to my application.i need a simple example with blocks to understand the multithreading using gcd.please help me...
Concurrency and multithreading are a core part of iOS development. Let's dive into what makes them so powerful, and how we can leverage them in our own Cocoa Touch applications. Concurrency is the notion of multiple things happening at the same time.
Apple provides 2 main APIs for writing multithreaded code: Grand Central Dispatch(GCD) and Operation. Behind the scenes, they use a concept known as thread pool, meaning that those API manages a large number of threads and when a task is ready to be executed, it grabs one thread from the pool to take care of the task.
Grand Central Dispatch (GCD) is a low-level API for managing concurrent operations. It can help improve your app's responsiveness by deferring computationally expensive tasks to the background. It's an easier concurrency model to work with than locks and threads.
Ok.. The most simple example )
You can write this code in any method. For example
-(void) viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Here your non-main thread.
NSLog (@"Hi, I'm new thread");
dispatch_async(dispatch_get_main_queue(), ^{
//Here you returns to main thread.
NSLog (@"Hi, I'm main thread");
});
});
}
Try this, its very clear and easy - http://en.wikipedia.org/wiki/Grand_Central_Dispatch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With