Upcoming SlideShare
×

# Palestra sobre Grand Central Dispatch no [self sp]

414 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
414
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
3
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Palestra sobre Grand Central Dispatch no [self sp]

1. 1. Diego Chohﬁ Turini Paralelismo transparente GCD(Grande Central Dispatcher) [self SP]; Diego Chohﬁ - @dchohﬁMonday, March 19, 12
2. 2. “Quando precisamos de paralelismo temos n problemas, e quando usamos temos n^2 problemas” - @qmx Diego Chohﬁ - @dchohﬁMonday, March 19, 12
3. 3. Primeiro: Blocos ^ Diego Chohﬁ - @dchohﬁMonday, March 19, 12
4. 4. Véi, na boa.. bloco? retorno paramêtro nome param int (^bloco)(int) = ^(int numero){ return numero *10;}; variável corpo do broco broco Diego Chohﬁ - @dchohﬁMonday, March 19, 12
5. 5. Bloco = tipo typedef double(^Dividir)(double, double); @property (copy) Dividir dividir; dividir = ^( double dividendo, double divisor){ return dividendo / divisor; }; NSLog(@"%f.0", self.dividir(10,2)); Diego Chohﬁ - @dchohﬁMonday, March 19, 12
6. 6. NSArray *OMG = [[NSArray alloc] initWithObjects:@"MOTHER", @"OF", @"JOBS", nil]; [OMG enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(obj); }]; TWTweetComposeViewController *tweet = [[TWTweetComposeViewController alloc] init]; [tweet setCompletionHandler:^(TWTweetComposeViewControllerResult result){ if(result == TWTweetComposeViewControllerResultDone){ NSLog(@"AI QUE LOUCURA, TWEETEI"); } }]; int variavelExterna = 10; void (^block)(int) = ^(int mother){ NSLog(@"OF JOBS %i, guardei escopo!", modern * variavelExterna); }; block(10); Diego Chohﬁ - @dchohﬁMonday, March 19, 12
7. 7. GCD, it’s all about queueing (based on kqueue) Diego Chohﬁ - @dchohﬁMonday, March 19, 12
8. 8. dispatch_queue_t queue = dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); #define! DISPATCH_QUEUE_PRIORITY_HIGH ! 2 #define! DISPATCH_QUEUE_PRIORITY_DEFAULT 0 #define! DISPATCH_QUEUE_PRIORITY_LOW ! (-2) #define! DISPATCH_QUEUE_PRIORITY_BACKGROUND! INT16_MIN dispatch_async(queue, ^{ NSLog(@"Calculando o tamanho do universo, no iPhone 3"); }); Diego Chohﬁ - @dchohﬁMonday, March 19, 12
9. 9. Criando queue’s dispatch_queue_t minhaQueue = dispatch_queue_create("br.com.caelum", NULL); dispatch_async(minhaQueue, ^{ }); Diego Chohﬁ - @dchohﬁMonday, March 19, 12
10. 10. Alterando views, cuidado! dispatch_async(queue, ^{ dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; [NSThread sleepForTimeInterval:2]; dispatch_async(dispatch_get_main_queue(), ^{ asyncLabel.text = = @"UNIVERSO CALCULADO"; asynchLabel.text @"UNIVERSO CALCULADO"; }); }); }); syncLabel.text = @"Calculando o universo"; syncLabel.text = @"Calculando o universo"; Diego Chohﬁ - @dchohﬁMonday, March 19, 12
11. 11. Criando Grupos dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group,queue,^{ [NSThread sleepForTimeInterval:1]; NSLog(@"Calculando o tamanho do sol"); }); dispatch_group_async(group,queue,^{ NSLog(@"Esperando a mulher fazer compras"); }); dispatch_group_notify(group,queue,^{ NSLog(@"Será executado após os dois blocos"); }); Diego Chohﬁ - @dchohﬁMonday, March 19, 12
12. 12. Valores fora do escopo int valorMagico = 0; __block dispatch_async(queue,^{ [NSThread sleepForTimeInterval:1]; for (int i=0; i<10; i++) { valorMagico++; } }); Diego Chohﬁ - @dchohﬁMonday, March 19, 12
13. 13. Batch queueing dispatch_queue_t queue = dispatch_get_global_queue(0, 0); dispatch_apply(10, queue, ^(size_t i){ NSLog(@"Numero: %zu", i);//não garante ordem }); Diego Chohﬁ - @dchohﬁMonday, March 19, 12