So, with some help, I more clearly understand how the nested GCD works in my program.
The original message is located at:
Make sure I correctly explain the nested GCD
However, you do not need to go through the original message, but basically the database runs in the background here, and the user interface responds:
-(void)viewDidLoad {
dispatch_queue_t concurrencyQueue = dispatch_queue_create("com.epam.halo.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t serialQueue = dispatch_queue_create("com.epam.halo.queue2", DISPATCH_QUEUE_SERIAL);
for ( int i = 0; i < 10; i++) {
dispatch_async(concurrencyQueue, ^() {
NSLog(@"START insertion method%d <--", i);
dispatch_sync(serialQueue, ^() {
NSLog(@"----------START %d---------", i);
[NSThread sleepForTimeInterval:1.0f];
NSLog(@"--------FINISHED %d--------", i);
});
NSLog(@"END insertion method%d <--", i);
});
}
}
However, when I start refactoring them and introducing them into methods and doing everything well, the user interface no longer responds:
// some class Singleton class
// ordinal queues are declared in a private class extension. And created in init ()
-(void)executeDatabaseStuff:(int)i {
dispatch_sync(serialQueue, ^() {
NSLog(@"----------START--------- %d", i);
[NSThread sleepForTimeInterval:1.0f];
NSLog(@"--------FINISHED-------- %d", i);
});
}
-(void)testInsert:(int)i {
dispatch_async(concurrencyQueue, ^() {
[self executeDatabaseStuff:i];
});
}
//ViewController.m
- (void)viewDidLoad {
for ( int i = 0; i < totalNumberOfPortfolios; i++) {
NSLog(@"START insertion method%d <--", i);
[[DatabaseFunctions sharedDatabaseFunctions] testInsert: i];
NSLog(@"END insertion method%d <--", i);
}
}
- dispatch_async (dispatch_get_main_queue():
for ( int i = 0; i < totalNumberOfPortfolios; i++) {
dispatch_async(dispatch_get_main_queue(), ^() {
NSLog(@"START insertion method%d <--", i);
[[DatabaseFunctions sharedDatabaseFunctions] testInsert: i];
NSLog(@"END insertion method%d <--", i);
});
}
, , , dispatch_async, concurrencyQueue , serial_query dispatch_sync. , /, dispatch_async (dispatch_get_main_queue()...)?
, dispatch_async
in viewDidLoad , .
, testInsert, . . , , dispatch_sync , viewDidLoad , testInsert , ?
, :
for ( int i = 0; i < 80; i++) {
NSLog(@"main thread %d <-- ", i);
dispatch_async(concurrencyQueue, ^() {
[NSThread isMainThread] ? NSLog(@"its the main thread") : NSLog(@"not main thread");
NSLog(@"concurrent Q thread %i <--", i);
dispatch_sync(serialQueue, ^() {
NSLog(@"serial Q thread ----------START %d---------", i);
[NSThread sleepForTimeInterval:1.0f];
NSLog(@"serial Q thread --------FINISHED %d--------", i);
});
NSLog(@"concurrent Q thread %i -->", i);
});
NSLog(@"main thread %d --> ", i);
}
1 63, . .
, 64, 1 , .
65, , ...
- 80, 64-80... 16 , .
, 64. , 64 .... /.: D
!