1
2
3#import <Foundation/Foundation.h>
4
5
6@interface BaseOperation : NSOperation
7
8@property (nonatomic, strong) NSString *operationName;
9@property (nonatomic, assign) BOOL isExecuting;
10@property (nonatomic, assign) BOOL isFinished;
11@property (nonatomic, assign) BOOL isCancelled;
12
13- (instancetype)initWithName:(NSString *)name;
14
15@end
16
17@implementation BaseOperation
18
19@synthesize isExecuting;
20@synthesize isFinished;
21@synthesize isCancelled;
22
23- (instancetype)initWithName:(NSString *)name {
24self = [super init];
25if (self) {
26_operationName = [name copy];
27isExecuting = NO;
28isFinished = NO;
29isCancelled = NO;
30}
31return self;
32}
33
34
35- (BOOL)isConcurrent {
36return YES;
37}
38
39- (void)start {
40
41if (self.isCancelled) {
42self.isFinished = YES;
43return;
44}
45
46
47self.isExecuting = YES;
48NSLog(@"%@: Starting execution.", self.operationName);
49
50
51
52
53
54[self performSelectorInBackground:@selector(main) withObject:nil];
55}
56
57
58
59- (void)main {
60@autoreleasepool {
61
62NSLog(@"%@: Performing work...", self.operationName);
63[NSThread sleepForTimeInterval:1.0];
64
65
66if (self.isCancelled) {
67NSLog(@"%@: Cancelled during work.", self.operationName);
68[self finish];
69return;
70}
71
72NSLog(@"%@: Work completed.", self.operationName);
73[self finish];
74}
75}
76
77
78- (void)finish {
79
80if (self.isFinished || self.isExecuting == NO) {
81return;
82}
83
84
85[self willChangeValueForKey:@"isExecuting"];
86self.isExecuting = NO;
87[self didChangeValueForKey:@"isExecuting"];
88
89[self willChangeValueForKey:@"isFinished"];
90self.isFinished = YES;
91[self didChangeValueForKey:@"isFinished"];
92
93NSLog(@"%@: Operation finished.", self.operationName);
94}
95
96
97- (void)cancel {
98[super cancel];
99self.isCancelled = YES;
100NSLog(@"%@: Cancel requested.", self.operationName);
101
102
103
104if (self.isExecuting) {
105
106
107}
108}
109
110@end
111
112
113@interface FetchDataOperation : BaseOperation
114@property (nonatomic, strong) id fetchedData;
115@end
116
117@implementation FetchDataOperation
118
119- (void)main {
120@autoreleasepool {
121NSLog(@"%@: Fetching data...", self.operationName);
122[NSThread sleepForTimeInterval:1.5];
123
124if (self.isCancelled) {
125NSLog(@"%@: Cancelled during data fetch.", self.operationName);
126[self finish];
127return;
128}
129
130
131self.fetchedData = @{@"key": @"value from fetch"};
132NSLog(@"%@: Data fetched successfully.", self.operationName);
133[self finish];
134}
135}
136
137@end
138
139
140@interface ProcessDataOperation : BaseOperation
141@property (nonatomic, strong) id inputData;
142@property (nonatomic, strong) id processedResult;
143
144- (instancetype)initWithName:(NSString *)name inputData:(id)data;
145
146@end
147
148@implementation ProcessDataOperation
149
150- (instancetype)initWithName:(NSString *)name inputData:(id)data {
151self = [super initWithName:name];
152if (self) {
153_inputData = data;
154}
155return self;
156}
157
158- (void)main {
159@autoreleasepool {
160NSLog(@"%@: Processing data...", self.operationName);
161[NSThread sleepForTimeInterval:1.0];
162
163if (self.isCancelled) {
164NSLog(@"%@: Cancelled during data processing.", self.operationName);
165[self finish];
166return;
167}
168
169
170if ([self.inputData isKindOfClass:[NSDictionary class]]) {
171self.processedResult = [NSString stringWithFormat:@"Processed: %@", self.inputData[@"key"]];
172} else {
173self.processedResult = @"Processed: Unknown Data";
174}
175NSLog(@"%@: Data processed.", self.operationName);
176[self finish];
177}
178}
179
180@end
181
182
183@interface SaveDataOperation : BaseOperation
184@property (nonatomic, strong) id dataToSave;
185
186- (instancetype)initWithName:(NSString *)name dataToSave:(id)data;
187
188@end
189
190@implementation SaveDataOperation
191
192- (instancetype)initWithName:(NSString *)name dataToSave:(id)data {
193self = [super initWithName:name];
194if (self) {
195_dataToSave = data;
196}
197return self;
198}
199
200- (void)main {
201@autoreleasepool {
202NSLog(@"%@: Saving data...", self.operationName);
203[NSThread sleepForTimeInterval:0.5];
204
205if (self.isCancelled) {
206NSLog(@"%@: Cancelled during save.", self.operationName);
207[self finish];
208return;
209}
210
211NSLog(@"%@: Data saved: %@", self.operationName, self.dataToSave);
212[self finish];
213}
214}
215
216@end
217
218
219void orchestrateTasks() {
220NSLog(@"Starting task orchestration...");
221
222NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
223operationQueue.maxConcurrentOperationCount = 2;
224
225
226FetchDataOperation *fetchOp = [[FetchDataOperation alloc] initWithName:@"Fetch Data"];
227ProcessDataOperation *processOp = [[ProcessDataOperation alloc] initWithName:@"Process Data" inputData:nil];
228SaveDataOperation *saveOp = [[SaveDataOperation alloc] initWithName:@"Save Data" dataToSave:nil];
229
230
231
232[processOp addDependency:fetchOp];
233
234[saveOp addDependency:processOp];
235
236
237[operationQueue addOperation:fetchOp];
238[operationQueue addOperation:processOp];
239[operationQueue addOperation:saveOp];
240
241
242
243[operationQueue addCompletionHandler:^{
244
245
246
247NSLog(@"All operations in queue have finished or been cancelled.");
248}];
249
250
251
252
253
254
255fetchOp.completionBlock = ^ {
256if (!fetchOp.isCancelled && fetchOp.fetchedData) {
257
258
259
260processOp.inputData = fetchOp.fetchedData;
261NSLog(@"Fetch operation completed. Setting input for Process operation.");
262} else {
263NSLog(@"Fetch operation was cancelled or failed. Process operation might not run.");
264
265
266if (fetchOp.isCancelled) {
267[processOp cancel];
268}
269}
270};
271
272
273processOp.completionBlock = ^ {
274if (!processOp.isCancelled && processOp.processedResult) {
275saveOp.dataToSave = processOp.processedResult;
276NSLog(@"Process operation completed. Setting input for Save operation.");
277} else {
278NSLog(@"Process operation was cancelled or failed. Save operation might not run.");
279if (processOp.isCancelled) {
280[saveOp cancel];
281}
282}
283};
284
285
286
287NSOperation *finalCompletionOp = [NSBlockOperation blockOperationWithBlock:^{
288NSLog(@"Final completion operation executed.");
289
290
291
292}];
293[finalCompletionOp addDependency:saveOp];
294[operationQueue addOperation:finalCompletionOp];
295
296
297
298
299
300
301
302NSLog(@"Operations added to queue. Max concurrent: %ld", (long)operationQueue.maxConcurrentOperationCount);
303
304}
305
306
307
308
309
310
311
312
313
314
315