forked from osolano/iOS-Design-Patterns-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithreadingViewController.m
More file actions
158 lines (124 loc) · 5.43 KB
/
Copy pathMultithreadingViewController.m
File metadata and controls
158 lines (124 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// MultithreadingViewController.m
// iOSFundamentalConcepts_iPad
//
// Created by Oliver Solano on 8/4/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "MultithreadingViewController.h"
#import <dispatch/dispatch.h>
@interface MultithreadingViewController ()
@end
@implementation MultithreadingViewController
@synthesize imageViewNormal, imageViewDispatch, imageViewDispatch2, imageViewDispatch3;
@synthesize toolbar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// self.imageViewNormal = [[UIImageView alloc] init];
// self.imageViewDispatch = [[UIImageView alloc] init];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
-(IBAction)selectNormalButton:(id)sender{
NSURL *networkURL = [NSURL URLWithString:@"http://oliversolano.com/sample_projects/weddingalbum/files/Image_4_H.png"];
NSData *imageData = [NSData dataWithContentsOfURL:networkURL]; // long time!
UIImage *image = [UIImage imageWithData:imageData];
self.imageViewNormal.image = image;
}
-(IBAction)selectDispatchButtonThread11:(id)sender{
NSURL *networkURL = [NSURL URLWithString:@"http://oliversolano.com/sample_projects/weddingalbum/files/Image_4_H.png"];
dispatch_queue_t downloadQueue = dispatch_queue_create("image loader", NULL);
dispatch_async(downloadQueue, ^{
NSData *imageData = [NSData dataWithContentsOfURL:networkURL]; // long time!
dispatch_async(dispatch_get_main_queue(), ^ {
UIImage *image = [UIImage imageWithData:imageData];
self.imageViewDispatch.image = image;
});
});
dispatch_release(downloadQueue);
}
-(IBAction)selectDispatchButtonThread1:(id)sender{
NSURL *networkURL = [NSURL URLWithString:@"http://oliversolano.com/sample_projects/weddingalbum/files/Image_10_P.png"];
//Although this is on the same labeled queue, it is still creates a new thread to download the data. This is no different than adding a thread on a different labeled queue.
dispatch_queue_t downloadQueue = dispatch_queue_create("image loader", NULL);
dispatch_async(downloadQueue, ^{
NSData *imageData1 = [NSData dataWithContentsOfURL:networkURL]; // long time!
dispatch_async(dispatch_get_main_queue(), ^ {
UIImage *image1 = [UIImage imageWithData:imageData1];
self.imageViewDispatch3.image = image1;
});
});
dispatch_release(downloadQueue);
}
-(IBAction)selectDispatchButtonThread2:(id)sender{
NSURL *networkURL = [NSURL URLWithString:@"http://oliversolano.com/sample_projects/weddingalbum/files/Image_4_H.png"];
dispatch_queue_t downloadQueue2 = dispatch_queue_create("image loader2", NULL);
dispatch_async(downloadQueue2, ^{
NSData *imageData = [NSData dataWithContentsOfURL:networkURL]; // long time!
dispatch_async(dispatch_get_main_queue(), ^ {
UIImage *image = [UIImage imageWithData:imageData];
self.imageViewDispatch2.image = image;
});
});
dispatch_release(downloadQueue2);
}
-(IBAction)selectDispatchButtonRunAllOnThread1:(id)sender{
NSURL *networkURL = [NSURL URLWithString:@"http://oliversolano.com/sample_projects/weddingalbum/files/Image_4_H.png"];
dispatch_queue_t downloadQueueAll = dispatch_queue_create("image load all", NULL);
dispatch_async(downloadQueueAll, ^{
NSData *imageData = [NSData dataWithContentsOfURL:networkURL]; // long time!
NSData *imageData2 = [NSData dataWithContentsOfURL:networkURL]; // long time!
NSData *imageData3 = [NSData dataWithContentsOfURL:networkURL]; // long time!
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
self.imageViewDispatch.image = image;
UIImage *image2 = [UIImage imageWithData:imageData2];
self.imageViewDispatch2.image = image2;
UIImage *image3 = [UIImage imageWithData:imageData3];
self.imageViewDispatch3.image = image3;
});
});
dispatch_release(downloadQueueAll);
}
-(IBAction)reset:(id)sender{
self.imageViewDispatch.image = nil;
self.imageViewNormal.image = nil;
self.imageViewDispatch2.image = nil;
self.imageViewDispatch3.image = nil;
// [self.imageViewNormal removeFromSuperview];
// [self.imageViewDispatch removeFromSuperview];
}
#pragma mark -
#pragma mark Managing the popover
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
// Add the popover button to the toolbar.
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray insertObject:barButtonItem atIndex:0];
[toolbar setItems:itemsArray animated:NO];
}
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
// Remove the popover button from the toolbar.
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray removeObject:barButtonItem];
[toolbar setItems:itemsArray animated:NO];
}
///Add methods that use NSOperations
@end