Detailed IOS multithreading GCD use _ios

Source: Internet
Author: User
Tags call back gcd

Grand-Dispatch (GCD) is one of the techniques for performing tasks asynchronously

The dispatch queue is divided into the following three types:

1 runs the main thread of the main queue and gets it through the dispatch_get_main_queue.

/*!
* @function Dispatch_get_main_queue
* * *
@abstract * Returns The default queue, this is bound to the
main thread .
*
@discussion
* In order to invoke blocks submitted to the main queue, the application must
* Call Dispatch_ Main (), Nsapplicationmain (), or use a cfrunloop on the main
* thread.
* *
@result
* Returns the main queue. This \ created automatically on behalf of
* 's main thread before main () is called.
* *
__osx_available_starting (__mac_10_6,__iphone_4_0)
dispatch_export struct dispatch_queue_s _dispatch_ Main_q;
#define DISPATCH_GET_MAIN_QUEUE () \
dispatch_global_object (dispatch_queue_t, _dispatch_main_q)

It can be seen that Dispatch_get_main_queue is also a kind of dispatch_queue_t.

2 Parallel Queue Global dispatch queue, obtained by Dispatch_get_global_queue, by the system to create three different priority dispatch queue. Parallel queues are executed in the same order as they were queued.

3 serial queue serial queues is typically used to synchronize access sequentially, creating any number of serial queues that are concurrent between each serial queue.

Serial queues are useful when you want tasks to be performed in a particular order. A serial queue performs only one task at a time. We can use serial queues instead of locks to protect shared data. Unlike locks, a serial queue can guarantee that a task executes in a predictable order.

Serial queues created by Dispatch_queue_create, you can use function Dispatch_retain and dispatch_release to increase or decrease the reference count.

Usage of GCD:

 Background execution: Dispatch_async (dispatch_get_global_queue (0, 0), ^{//Something});

 Main thread execution: Dispatch_async (Dispatch_get_main_queue (), ^{//Something});
 One-time execution: static dispatch_once_t Oncetoken;

 Dispatch_once (&oncetoken, ^{/code to be executed once});
 Delay 2 seconds execution: double delayinseconds = 2.0;
 dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, delayinseconds * nsec_per_sec);

 Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {//code to is executed on the main queue after delay});
 Custom dispatch_queue_t dispatch_queue_t urls_queue = dispatch_queue_create ("blog.devtang.com", NULL);
 Dispatch_async (Urls_queue, ^{//Your code});

 Dispatch_release (Urls_queue);
 Merge Rollup results dispatch_group_t group = dispatch_group_create ();
 Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{//parallel execution of Threads one});
 Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{//Parallel thread Execution two}); Dispatch_group_notify (Group, Dispatch_get_globaL_queue (0,0), ^{//Rollup results});
 

An example of applying GCD:

 Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
  nsurl * url = [NSURL urlwithstring:@ "http://www.baidu.com"];
  Nserror * ERROR;
  NSString * data = [NSString stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&error];
  if (data!= nil) {
   Dispatch_async (Dispatch_get_main_queue (), ^{
    NSLog (@ "Call back, the data is:%@", data);
   );
  } else {
   NSLog (@ "error when download:%@", error);
  }
 );

Another use of GCD is to allow programs to run longer in the background.

When GCD is not used, the app has a maximum of 5 seconds to do some saving or cleanup work when the app is pulled out of the home key. But after using GCD, the app has a maximum of 10 minutes to run in the background for a long time. This time can be used to clean the local cache, send statistics, and so on.

The sample code to keep the program running in the background is as follows:

AppDelegate.h file
@property (assign, nonatomic) Uibackgroundtaskidentifier backgroundupdatetask;

APPDELEGATE.M file
-(void) Applicationdidenterbackground: (uiapplication *) application
{
 [self Beingbackgroundupdatetask];
 Add here the code you need to run long
 [self endbackgroundupdatetask];
}

-(void) beingbackgroundupdatetask
{
 self.backgroundupdatetask = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{
  [self endbackgroundupdatetask];}
 ]

-(void) endbackgroundupdatetask
{
 [[uiapplication sharedapplication] Endbackgroundtask: Self.backgroundupdatetask];
 Self.backgroundupdatetask = Uibackgroundtaskinvalid;
}

The above content is small make up to everybody introduces iOS in GCD use, hope to help everybody!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.