The main thread executes dispatch_async in a parallel queue in viewDidLoad or as part of a method

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, ^() {
            //this is to simulate writing to database
            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, ^() {
       //this is to simulate writing to database
       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 {

    //UI is unresponsive :(
    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, ^() {

                //this is to simulate writing to database
                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);
    } //for loop

1 63, . .

, 64, 1 , .

65, , ...

- 80, 64-80... 16 , .

, 64. , 64 .... /.: D

!

+4
1

64 GCD ( ), .

, 64 , [NSThread sleepForTimeInterval:1.0f], . , , .

100 " " ( ), , -, 36 ( 64 , , ).

singleton , , .

- " ". .

- (void)viewDidLoad {
    [super viewDidLoad];

    static dispatch_once_t t;

    dispatch_once(&t, ^{
        serialQueue = dispatch_queue_create("com.epam.halo.queue2", DISPATCH_QUEUE_SERIAL);
    });


    for (int i = 0; i < 100; i++) {
        [self testInsert:i];
    }

}

-(void)executeDatabaseStuff:(int)i {
    //this is to simulate writing to database
    NSLog(@"----------START--------- %d", i);
    [NSThread sleepForTimeInterval:1.0f];
    NSLog(@"--------FINISHED-------- %d", i);
}

-(void)testInsert:(int)i {
    NSLog(@"Start insert.... %d", i);
    dispatch_async(serialQueue, ^() {
        [self executeDatabaseStuff:i];
    });
    NSLog(@"End insert... %d", i);
}

, dispatch_async(dispatch_get_main_queue(), ^() {} for for... , " " , .

GCD

+3

Source: https://habr.com/ru/post/1624739/


All Articles