Skip to content

Commit 08c89a7

Browse files
committed
Better control around cancellation
1 parent ebb489d commit 08c89a7

5 files changed

Lines changed: 44 additions & 39 deletions

File tree

NetworkingWingman-Example/Managers/Queue/NWMOperationQueueManager.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ @implementation NWMOperationQueueManager
2424
+ (instancetype)sharedInstance
2525
{
2626
static NWMOperationQueueManager *sharedInstance = nil;
27-
27+
2828
static dispatch_once_t onceToken;
2929
dispatch_once(&onceToken, ^
3030
{
3131
sharedInstance = [[NWMOperationQueueManager alloc] init];
3232
});
33-
33+
3434
return sharedInstance;
3535
}
3636

@@ -39,12 +39,12 @@ + (instancetype)sharedInstance
3939
- (instancetype)init
4040
{
4141
self = [super init];
42-
42+
4343
if (self)
4444
{
4545
self.queue = [[NSOperationQueue alloc] init];
4646
}
47-
47+
4848
return self;
4949
}
5050

NetworkingWingman-Example/Networking/Abstract/Operation/NWMOperation.m

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
@implementation NWMOperation
1212

13-
/*
14-
We need to do old school synthesizing as the compiler has trouble creating the internal ivars.
15-
*/
1613
@synthesize ready = _ready;
1714
@synthesize executing = _executing;
1815
@synthesize finished = _finished;
@@ -22,12 +19,12 @@ @implementation NWMOperation
2219
- (instancetype)init
2320
{
2421
self = [super init];
25-
22+
2623
if (self)
2724
{
2825
self.ready = YES;
2926
}
30-
27+
3128
return self;
3229
}
3330

@@ -45,7 +42,7 @@ - (void)setReady:(BOOL)ready
4542

4643
- (BOOL)isReady
4744
{
48-
return _ready;
45+
return [super isReady] && _ready;
4946
}
5047

5148
- (void)setExecuting:(BOOL)executing
@@ -78,34 +75,49 @@ - (BOOL)isFinished
7875
return _finished;
7976
}
8077

81-
- (BOOL)isAsynchronous
82-
{
83-
return YES;
84-
}
85-
86-
#pragma mark - Control
78+
#pragma mark - Start
8779

8880
- (void)start
8981
{
82+
if (self.isCancelled)
83+
{
84+
[self finish];
85+
return;
86+
}
87+
9088
if (!self.isExecuting)
9189
{
9290
self.ready = NO;
9391
self.executing = YES;
9492
self.finished = NO;
95-
96-
NSLog(@"\"%@\" Operation Started.", self.name);
9793
}
9894
}
9995

96+
#pragma mark - Finish
97+
10098
- (void)finish
10199
{
102100
if (self.executing)
103101
{
104-
NSLog(@"\"%@\" Operation Finished.", self.name);
105-
106102
self.executing = NO;
107103
self.finished = YES;
108104
}
109105
}
110106

107+
#pragma mark - Cancel
108+
109+
- (void)cancel
110+
{
111+
[super cancel];
112+
113+
[self finish];
114+
}
115+
116+
#pragma mark - Asynchronous
117+
118+
- (BOOL)isAsynchronous
119+
{
120+
return YES;
121+
}
122+
111123
@end

NetworkingWingman-Example/Networking/Answers/NWMAnswersAPIManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ @implementation NWMAnswersAPIManager
1818
+ (void)retrieveAnswersWithCompletion:(void (^)(NSArray *answers))completion
1919
{
2020
NWMAnswersRetrievalOperation *operation = [[NWMAnswersRetrievalOperation alloc] initWithCompletion:completion];
21-
21+
2222
[[NWMOperationQueueManager sharedInstance] addOperation:operation];
2323
}
2424

NetworkingWingman-Example/Networking/Answers/Operations/Retrieval/NWMAnswersRetrievalOperation.m

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "NWMAnswersRetrievalOperation.h"
10+
1011
#import "NWMAnswerParser.h"
1112

1213
@interface NWMAnswersRetrievalOperation ()
@@ -25,13 +26,13 @@ @implementation NWMAnswersRetrievalOperation
2526
- (instancetype)initWithCompletion:(void (^)(NSArray *answers))completion
2627
{
2728
self = [super init];
28-
29+
2930
if (self)
3031
{
3132
self.completion = completion;
3233
self.name = @"Answers-Retrieval";
3334
}
34-
35+
3536
return self;
3637
}
3738

@@ -40,38 +41,30 @@ - (instancetype)initWithCompletion:(void (^)(NSArray *answers))completion
4041
- (void)start
4142
{
4243
[super start];
43-
44+
4445
NSURLSession *session = [NSURLSession sharedSession];
45-
46+
4647
NSURL *url = [NSURL URLWithString:@"https://api.stackexchange.com/2.2/answers?site=stackoverflow"];
47-
48+
4849
NSURLSessionDataTask *task = [session dataTaskWithURL:url
4950
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
5051
{
5152
NSDictionary *answersJSON = [NSJSONSerialization JSONObjectWithData:data
5253
options:NSJSONReadingMutableContainers
5354
error:nil];
54-
55+
5556
NWMAnswerParser *parser = [[NWMAnswerParser alloc] init];
5657
NSArray *answers = [parser parseAnswers:answersJSON[@"items"]];
57-
58+
5859
if (self.completion)
5960
{
6061
self.completion(answers);
6162
}
62-
63+
6364
[self finish];
6465
}];
65-
66+
6667
[task resume];
6768
}
6869

69-
#pragma mark - Cancel
70-
71-
- (void)cancel
72-
{
73-
[super cancel];
74-
75-
[self finish];
76-
}
7770
@end

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![Build](https://github.com/wibosco/NetworkingWingman-Example/actions/workflows/objective-c-xcode.yml/badge.svg)](https://github.com/wibosco/NetworkingWingman-Example/actions/workflows/objective-c-xcode.yml)
2-
<a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011210"><img src="https://img.shields.io/badge/Objective--c-language-blue" alt="Objective-C" /></a>
2+
<a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011210"><img src="https://img.shields.io/badge/Objective--C-2.0-blue" alt="Objective-C" /></a>
33
[![License](http://img.shields.io/badge/License-MIT-green.svg?style=flat)](https://github.com/wibosco/NetworkingWingman-Example/blob/main/LICENSE)
44

55
# NetworkingWingman-Example

0 commit comments

Comments
 (0)