1 Preface
When executing tasks unrelated to the UI or interacting with the UI, like executing other tasks, it takes a lot of time and the above situations often occur. We can use the dispatch_sync function to execute synchronization tasks on a dispatch queue. All you have to do is provide a handle to this queue. This queue must run tasks and a code block will be executed on this queue. Today we will learn how to perform non-UI operations on GCD.
2. code example
TestDemo. h
[Plain]
# Import <Foundation/Foundation. h>
@ Interface TestDemo: NSObject
-(Void) testMethod;
-(Void) testMethod2;
@ End
# Import <Foundation/Foundation. h>
@ Interface TestDemo: NSObject
-(Void) testMethod;
-(Void) testMethod2;
@ End
TestDemo. m
[Plain]
# Import "TestDemo. h"
@ Implementation TestDemo
/************** Objc Method Start *********/
// Block Object
Void (^ printFrom1To1000) (void) = ^ {
NSUInteger counter = 0;
For (counter = 1; counter <= 1000; counter ++ ){
NSLog (@ "Counter = % lu-Thread = % @",
(Unsigned long) counter,
[NSThread currentThread]);
}
};
// Test Method
-(Void) testMethod {
/*
The first parameter of the Dispatch_get_global_queue function specifies the priority of the concurrent queue. This attribute GCD must be checked by the programmer.
. The higher the priority, more CPU Timeslice will be provided to obtain the code executed by the queue.
The first parameter of the Dispatch_get_global_queue function:
DISPATCH_QUEUE_PRIORITY_LOW
Your Tasks use less Timeslice than normal tasks.
DISPATCH_QUEUE_PRIORITY_DEFAULT
The default system priority for code execution is applied to your tasks.
DISPATCH_QUEUE_PRIORITY_HIGH
Compared with normal tasks, more Timeslices will be applied to your tasks.
The second parameter of the Dispatch_get_global_queue function has been saved. You only need to input a value 0 to it.
*/
Dispatch_queue_t concurrentQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
Dispatch_sync (concurrentQueue, printFrom1To1000 );
Dispatch_sync (concurrentQueue, printFrom1To1000 );
}
/************* Objc Method End *********/
/************** C Method Start *********/
// Block Object
Void print2From1To1000 (void * paramContext ){
NSUInteger counter = 0; for (counter = 1; counter <= 1000; counter ++ ){
NSLog (@ "Counter = % lu-Thread = % @",
(Unsigned long) counter, [NSThread currentThread]);
}
}
// Test Method
-(Void) testMethod2 {
/*
*/
Dispatch_queue_t concurrentQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
Dispatch_sync_f (concurrentQueue, NULL, print2From1To1000 );
Dispatch_sync_f (concurrentQueue, NULL, print2From1To1000 );
}
/************** C Method End *********/
@ End
# Import "TestDemo. h"
@ Implementation TestDemo
/************** Objc Method Start *********/
// Block Object
Void (^ printFrom1To1000) (void) = ^ {
NSUInteger counter = 0;
For (counter = 1; counter <= 1000; counter ++ ){
NSLog (@ "Counter = % lu-Thread = % @",
(Unsigned long) counter,
[NSThread currentThread]);
}
};
// Test Method
-(Void) testMethod {
/*
The first parameter of the Dispatch_get_global_queue function specifies the priority of the concurrent queue. This attribute GCD must be checked by the programmer.
. The higher the priority, more CPU Timeslice will be provided to obtain the code executed by the queue.
The first parameter of the Dispatch_get_global_queue function:
DISPATCH_QUEUE_PRIORITY_LOW
Your Tasks use less Timeslice than normal tasks.
DISPATCH_QUEUE_PRIORITY_DEFAULT
The default system priority for code execution is applied to your tasks.
DISPATCH_QUEUE_PRIORITY_HIGH
Compared with normal tasks, more Timeslices will be applied to your tasks.
The second parameter of the Dispatch_get_global_queue function has been saved. You only need to input a value 0 to it.
*/
Dispatch_queue_t concurrentQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
Dispatch_sync (concurrentQueue, printFrom1To1000 );
Dispatch_sync (concurrentQueue, printFrom1To1000 );
}
/************* Objc Method End *********/
/************** C Method Start *********/
// Block Object
Void print2From1To1000 (void * paramContext ){
NSUInteger counter = 0; for (counter = 1; counter <= 1000; counter ++ ){
NSLog (@ "Counter = % lu-Thread = % @",
(Unsigned long) counter, [NSThread currentThread]);
}
}
// Test Method
-(Void) testMethod2 {
/*
*/
Dispatch_queue_t concurrentQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
Dispatch_sync_f (concurrentQueue, NULL, print2From1To1000 );
Dispatch_sync_f (concurrentQueue, NULL, print2From1To1000 );
}
/************** C Method End *********/
@ End
Main. m
[Plain]
Import <Foundation/Foundation. h>
# Import "TestDemo. h"
Int main (int argc, const char * argv [])
{
@ Autoreleasepool {
TestDemo * test = [[TestDemo alloc] init];
// [Test testMethod];
[Test testMethod2];
[Test release];
}
Return 0;
}
# Import <Foundation/Foundation. h>
# Import "TestDemo. h"
Int main (int argc, const char * argv [])
{
@ Autoreleasepool {
TestDemo * test = [[TestDemo alloc] init];
// [Test testMethod];
[Test testMethod2];
[Test release];
}
Return 0;
}
Running result
13:50:37. 361 GCDExecuteNonUITest [556: 303] Counter = 1-Thread = <NSThread: 0x10010b2c0> {name = (null), num = 1}
13:50:37. 363 GCDExecuteNonUITest [556: 303] Counter = 2-Thread = <NSThread: 0x10010b2c0> {name = (null), num = 1}
...
13:50:38. 255 GCDExecuteNonUITest [556: 303] Counter = 1-Thread = <NSThread: 0x10010b2c0> {name = (null), num = 1}
13:50:38. 256 GCDExecuteNonUITest [556: 303] Counter = 2-Thread = <NSThread: 0x10010b2c0> {name = (null), num = 1}