On the recent tech. The interview asked the question "There are three streams: 2 asynchronous and 1 synchronous, how do they need to be executed so that at the end of the first 2 asynchronous, 1 synchronous is executed?"
- oneCan I not understand how a stream can be synchronous or asynchronous? - Max Mikheyenko 2:49
- maybe I didn’t explain, do 2 dispatch_async and 1 dispatch_sync. - Seryozha Kvyatkovskiy
|
1 answer
You need to create a group of asynchronous streams. And after it ends, call the synchronous flow.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ for (int i = 1; i < 9000; ++i) { NSLog(@"dispatch_async - 1: %i", i); }; }); dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ for (int i = 1; i < 9000; ++i) { NSLog(@"dispatch_async - 2: %i", i); }; }); dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{ dispatch_sync(dispatch_get_global_queue(0, 0), ^{ for (int i = 1; i < 9000; ++i) { NSLog(@"dispatch_sync: %i", i); }; }); }); NSLog(@"not dispatched code ****"); } |