2013-05-15 threads. why and how
TRANSCRIPT
![Page 1: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/1.jpg)
Why we should use thread and how to do it with no additional efforts.
![Page 2: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/2.jpg)
Dominik Haska – Research Software Developer in Jeppesen a Boeing Company
9 years of professional programming including 1 year in IOS
![Page 3: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/3.jpg)
Why ◦ Moore law and physics limitations
How ◦ how to use the potentially power of multicore
devices easily and properly
![Page 4: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/4.jpg)
![Page 5: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/5.jpg)
![Page 6: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/6.jpg)
![Page 7: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/7.jpg)
iPhone 1 – 1 CPU 412 MHz
iPhone 5 – 2 CPU 1300 MHz
iPad 1 – 1 CPU 1000 MHz
iPad 4 (retina) – 2 CPU 1400 MHz
![Page 8: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/8.jpg)
all animations: ◦ GPU is a massive multiprocessor unit
◦ Just use the animation block
[UIView animateWithDuration:1.0 animations:^{
firstView.alpha = 0.0;
secondView.alpha = 1.0;
}];
![Page 9: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/9.jpg)
[UIView beginAnimations:@"ToggleViews" context:nil];
[UIView setAnimationDuration:1.0];
// Make the animatable changes.
firstView.alpha = 0.0;
secondView.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
![Page 10: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/10.jpg)
Animation block stops user interaction during animation
animateWithDuration:delay: options: animations:completion:
UIViewAnimationOptions: ◦ UIViewAnimationOptionAllowUserInteraction
◦ UIViewAnimationOptionBeginFromCurrentState
[self.layer removeAllAnimations];
![Page 11: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/11.jpg)
CGRect animatedFrame = [self.animatedView.layer.presentationLayer frame];
CGRect targetFrame = self. animatedView.frame;
if(animatedFrame.origin.x != 0.0 || animatedFrame.origin.y != 0.0){ CGFloat xMis = animatedFrame.origin.x –
targetFrame.origin.x; CGFloat yMis = animatedFrame.origin.y - targetFrame.origin.y; [fix the position]; }
![Page 12: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/12.jpg)
Create property with quee
◦ [NSOperationQueue new];
Create invocation
◦ NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(calculate) object:nil];
Add operation to quee
◦ [self.renderingQueue addOperation:operation];
Or remove (cancelAllOperations)
![Page 13: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/13.jpg)
Some code have to be executed in main thread ◦ [NSString sizeWithFont] – sometimes – once per 1-2 day crashes app
◦ [[UIView alloc] init] ◦ [view addSubview…]
Solution ◦ Delegate and perform on main thread
performSelectorOnMainThread:@selector(
◦ Use NSAttributedString to work with UIKit Additions ◦ Always check when app crashes in which thread are
you
![Page 14: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/14.jpg)
dispatch_async(dispatch_get_global_qu
eue(DISPATCH_QUEUE_PRIORITY_DEFAULT),
^{
your code here
}
![Page 15: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/15.jpg)
Long time for response
Multiple resources needed in parraler (webbrowsers)
Ready to use in ios:
[SSLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse*
response, NSData* data, NSError*
error){ … }
![Page 16: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/16.jpg)
You have multicore devices – use this Look for simplest solution – use library as
much as possible Focus on user interaction – do not freeze your
apps Be carefull with debugging – single thread
app is much more testable – much much more
I work for jeppesen boeing company – if i show you sample code I have to kill you ;) – but next time I try to be better
![Page 17: 2013-05-15 threads. why and how](https://reader034.vdocuments.mx/reader034/viewer/2022042607/554dd5a4b4c905d10e8b4c13/html5/thumbnails/17.jpg)
developer.apple.com
http://www.gotw.ca/publications/concurrency-ddj.htm - the free lunch is over
http://www.robotswillstealyourjob.com/ - robots are stealing your job – and engineers help them to
http://www.extremetech.com/computing/116561-the-death-of-cpu-scaling-from-one-core-to-many-and-why-were-still-stuck/2