Skip to content

Commit

Permalink
Add cancel all operations method in base datasource.
Browse files Browse the repository at this point in the history
  • Loading branch information
grigaci committed Jun 22, 2016
1 parent 231e530 commit a7d88fc
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 40 deletions.
36 changes: 32 additions & 4 deletions BIObjCHelpers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
43D3BFF31D12E0DB000C840C /* MockNSFetchedResultsController.m in Sources */ = {isa = PBXBuildFile; fileRef = 43D3BFF21D12E0DB000C840C /* MockNSFetchedResultsController.m */; };
43D3BFF71D12E592000C840C /* MockUINib.m in Sources */ = {isa = PBXBuildFile; fileRef = 43D3BFF61D12E592000C840C /* MockUINib.m */; };
43D3BFFC1D12FCC0000C840C /* MockBITableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 43D3BFFB1D12FCC0000C840C /* MockBITableView.m */; };
43D3BFFE1D1A8080000C840C /* BIOperationNotifierTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 43D3BFFD1D1A8080000C840C /* BIOperationNotifierTestCase.m */; };
43D3C0021D1A8143000C840C /* MockBIOperationNotifierListener.m in Sources */ = {isa = PBXBuildFile; fileRef = 43D3C0011D1A8143000C840C /* MockBIOperationNotifierListener.m */; };
43D3C0081D1A832C000C840C /* BIDatasourceBaseTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 43D3C0071D1A832C000C840C /* BIDatasourceBaseTestCase.m */; };
43F541CD1CD0F276002EB6C6 /* BIObjCHelpers.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43F541C31CD0F275002EB6C6 /* BIObjCHelpers.framework */; };
43F541DC1CD0F2AD002EB6C6 /* _BIScrollViewProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 91614A821B94790400D00EB2 /* _BIScrollViewProxy.m */; };
43F541DE1CD0F2AD002EB6C6 /* UIScrollView+BIBatching.m in Sources */ = {isa = PBXBuildFile; fileRef = 432001971C7B26C0006A8BB7 /* UIScrollView+BIBatching.m */; };
Expand Down Expand Up @@ -105,7 +108,6 @@
43FA5D591CD2323B00FA754E /* NSDateBIAttributedStringTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 43BE6CDC1B568ECB001F0A00 /* NSDateBIAttributedStringTestCase.m */; };
43FA5D5A1CD2323E00FA754E /* NSStringBIExtraTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 43BE6CDE1B568ECB001F0A00 /* NSStringBIExtraTestCase.m */; };
43FA5D5B1CD2324300FA754E /* BIDatasourceFetchedCollectionViewTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 43BE6CE11B568ECB001F0A00 /* BIDatasourceFetchedCollectionViewTestCase.m */; };
43FA5D5D1CD2324D00FA754E /* BIDatasourceBaseTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 43329F6E1BFC7D6F0081FDA3 /* BIDatasourceBaseTestCase.m */; };
43FA5D5E1CD2325200FA754E /* BIDatasourceTableViewTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 4304A9B51B70D892009739B3 /* BIDatasourceTableViewTestCase.m */; };
43FA5D5F1CD2325400FA754E /* BIDatasourceFeedTableViewTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 43BE6CE61B568ECB001F0A00 /* BIDatasourceFeedTableViewTestCase.m */; };
43FA5D601CD2325700FA754E /* BIDatasourceFetchedTableViewTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 43BE6CE71B568ECB001F0A00 /* BIDatasourceFetchedTableViewTestCase.m */; };
Expand Down Expand Up @@ -136,7 +138,6 @@
431124911CE36CDC00950918 /* BIOperationNotifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BIOperationNotifier.m; path = Operations/BIOperationNotifier.m; sourceTree = "<group>"; };
432001961C7B26C0006A8BB7 /* UIScrollView+BIBatching.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIScrollView+BIBatching.h"; path = "Views/ScrollView/UIScrollView+BIBatching.h"; sourceTree = "<group>"; };
432001971C7B26C0006A8BB7 /* UIScrollView+BIBatching.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIScrollView+BIBatching.m"; path = "Views/ScrollView/UIScrollView+BIBatching.m"; sourceTree = "<group>"; };
43329F6E1BFC7D6F0081FDA3 /* BIDatasourceBaseTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BIDatasourceBaseTestCase.m; sourceTree = "<group>"; };
433DEB481C4D1A16002296AE /* BIScrollAdditionalViewBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BIScrollAdditionalViewBase.h; path = Views/TableView/Additions/BIScrollAdditionalViewBase.h; sourceTree = "<group>"; };
433DEB491C4D1A16002296AE /* BIScrollAdditionalViewBase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BIScrollAdditionalViewBase.m; path = Views/TableView/Additions/BIScrollAdditionalViewBase.m; sourceTree = "<group>"; };
433DEB4B1C4D1A52002296AE /* BIScrollAdditionalErrorNoContentView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BIScrollAdditionalErrorNoContentView.h; path = Views/TableView/Additions/BIScrollAdditionalErrorNoContentView.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -221,6 +222,11 @@
43D3BFF61D12E592000C840C /* MockUINib.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MockUINib.m; path = Mocks/UIKit/UINib/MockUINib.m; sourceTree = "<group>"; };
43D3BFFA1D12FCC0000C840C /* MockBITableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MockBITableView.h; path = Mocks/Views/TableView/MockBITableView.h; sourceTree = "<group>"; };
43D3BFFB1D12FCC0000C840C /* MockBITableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MockBITableView.m; path = Mocks/Views/TableView/MockBITableView.m; sourceTree = "<group>"; };
43D3BFFD1D1A8080000C840C /* BIOperationNotifierTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BIOperationNotifierTestCase.m; path = Operations/BIOperationNotifierTestCase.m; sourceTree = "<group>"; };
43D3C0001D1A8143000C840C /* MockBIOperationNotifierListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MockBIOperationNotifierListener.h; path = Mocks/Operations/MockBIOperationNotifierListener.h; sourceTree = "<group>"; };
43D3C0011D1A8143000C840C /* MockBIOperationNotifierListener.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MockBIOperationNotifierListener.m; path = Mocks/Operations/MockBIOperationNotifierListener.m; sourceTree = "<group>"; };
43D3C0031D1A8287000C840C /* BIOperationNotifier+Tests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "BIOperationNotifier+Tests.h"; path = "Operations/BIOperationNotifier+Tests.h"; sourceTree = "<group>"; };
43D3C0071D1A832C000C840C /* BIDatasourceBaseTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BIDatasourceBaseTestCase.m; path = Base/BIDatasourceBaseTestCase.m; sourceTree = "<group>"; };
43DA594B1B6217F400ECAAC0 /* BITableViewTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BITableViewTestCase.m; path = Views/TableView/BITableViewTestCase.m; sourceTree = "<group>"; };
43F23F7C1B9EFC0D00CEAD8C /* BIBatchHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BIBatchHelpers.h; sourceTree = "<group>"; };
43F23F7D1B9EFC0D00CEAD8C /* BIBatchHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BIBatchHelpers.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -318,6 +324,8 @@
isa = PBXGroup;
children = (
43A5B5561CFD733300F79359 /* BIOperationBaseTestCase.m */,
43D3BFFD1D1A8080000C840C /* BIOperationNotifierTestCase.m */,
43D3C0031D1A8287000C840C /* BIOperationNotifier+Tests.h */,
);
name = Operations;
sourceTree = "<group>";
Expand Down Expand Up @@ -669,6 +677,7 @@
43BE6CDF1B568ECB001F0A00 /* Datasource */ = {
isa = PBXGroup;
children = (
43D3C0061D1A8315000C840C /* Base */,
43BE6CE01B568ECB001F0A00 /* CollectionView */,
43BE6CE51B568ECB001F0A00 /* TableView */,
);
Expand All @@ -686,7 +695,6 @@
43BE6CE51B568ECB001F0A00 /* TableView */ = {
isa = PBXGroup;
children = (
43329F6E1BFC7D6F0081FDA3 /* BIDatasourceBaseTestCase.m */,
4304A9B51B70D892009739B3 /* BIDatasourceTableViewTestCase.m */,
43BE6CE61B568ECB001F0A00 /* BIDatasourceFeedTableViewTestCase.m */,
43BE6CE71B568ECB001F0A00 /* BIDatasourceFetchedTableViewTestCase.m */,
Expand Down Expand Up @@ -751,6 +759,7 @@
43D3BFD81D12DCA6000C840C /* Mocks */ = {
isa = PBXGroup;
children = (
43D3BFFF1D1A8127000C840C /* Operations */,
43D3BFF81D12FCA4000C840C /* Views */,
43D3BFE71D12DF5D000C840C /* Handlers */,
43D3BFDA1D12DCAD000C840C /* UIKit */,
Expand Down Expand Up @@ -859,6 +868,23 @@
name = TableView;
sourceTree = "<group>";
};
43D3BFFF1D1A8127000C840C /* Operations */ = {
isa = PBXGroup;
children = (
43D3C0001D1A8143000C840C /* MockBIOperationNotifierListener.h */,
43D3C0011D1A8143000C840C /* MockBIOperationNotifierListener.m */,
);
name = Operations;
sourceTree = "<group>";
};
43D3C0061D1A8315000C840C /* Base */ = {
isa = PBXGroup;
children = (
43D3C0071D1A832C000C840C /* BIDatasourceBaseTestCase.m */,
);
name = Base;
sourceTree = "<group>";
};
43DA58F21B5D1CAB00ECAAC0 /* ActivityIndicator */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1185,7 +1211,6 @@
43FA5D541CD2321E00FA754E /* BITableViewTestCase.m in Sources */,
43FA5D661CD2326F00FA754E /* BIStartersFactoryTestCase.m in Sources */,
43D3BFE21D12DD5B000C840C /* MockUITableView.m in Sources */,
43FA5D5D1CD2324D00FA754E /* BIDatasourceBaseTestCase.m in Sources */,
43D3BFE61D12DDC1000C840C /* MockUICollectionView.m in Sources */,
43FA5D601CD2325700FA754E /* BIDatasourceFetchedTableViewTestCase.m in Sources */,
43FA5D621CD2326000FA754E /* BIHandlerTableViewTestCase.m in Sources */,
Expand All @@ -1200,10 +1225,13 @@
43D3BFFC1D12FCC0000C840C /* MockBITableView.m in Sources */,
43FA5D571CD2322900FA754E /* BIBatchHelpersTestCase.m in Sources */,
43FA5D591CD2323B00FA754E /* NSDateBIAttributedStringTestCase.m in Sources */,
43D3C0081D1A832C000C840C /* BIDatasourceBaseTestCase.m in Sources */,
43FA5D641CD2326800FA754E /* BISerialOperationQueueTestCase.m in Sources */,
43D3C0021D1A8143000C840C /* MockBIOperationNotifierListener.m in Sources */,
43FA5D551CD2322400FA754E /* BIBatchRequestTestCase.m in Sources */,
43FA5D561CD2322700FA754E /* BIBatchResponseTestCase.m in Sources */,
43D3BFF31D12E0DB000C840C /* MockNSFetchedResultsController.m in Sources */,
43D3BFFE1D1A8080000C840C /* BIOperationNotifierTestCase.m in Sources */,
43FA5D681CD2327600FA754E /* BIMockStarter.m in Sources */,
43FA5D581CD2323800FA754E /* NSBundleBIExtraTestCase.m in Sources */,
43D3BFEF1D12E0B1000C840C /* MockNSFetchedResultsSectionInfo.m in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions BIObjCHelpers/Datasource/Base/BIDatasourceBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

#import "BILifecycle.h"

#import <UIKit/UIKit.h>

@interface BIDatasourceBase : BILifecycle

@property (nonatomic, strong, nonnull, readonly) NSHashTable *operations;

- (void)cancelAllCurrentOperations:(BOOL)silently;

@end
22 changes: 22 additions & 0 deletions BIObjCHelpers/Datasource/Base/BIDatasourceBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

#import "BIDatasourceBase.h"
#import "BIOperationBase.h"
#import "BIOperationNotifier.h"

@interface BIDatasourceBase ()

Expand All @@ -17,6 +19,26 @@ @interface BIDatasourceBase ()

@implementation BIDatasourceBase

#pragma mark - Public methods

- (void)cancelAllCurrentOperations:(BOOL)silently {
NSHashTable *copiedOperations = [self.operations copy];
for (id object in copiedOperations) {
if ([object isKindOfClass:[BIOperationBase class]]) {
BIOperationBase *operation = (BIOperationBase *)object;
if (silently) {
operation.didFinishWithErrorCallback = nil;
if ([operation isKindOfClass:[BIOperationNotifier class]]) {
BIOperationNotifier *notifierOperation = (BIOperationNotifier *)operation;
[notifierOperation removeAllListeners];
}
}
[operation cancel];
}
}
[self.operations removeAllObjects];
}

#pragma mark - Property methods

- (NSHashTable *)operations {
Expand Down
1 change: 1 addition & 0 deletions BIObjCHelpers/Operations/BIOperationNotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@

- (void)registerOperationFinishedListener:(id<BIOperationNotifierListener> __nonnull)listener;
- (void)unregisterOperationFinishedListener:(id<BIOperationNotifierListener> __nonnull)listener;
- (void)removeAllListeners;

@end
4 changes: 4 additions & 0 deletions BIObjCHelpers/Operations/BIOperationNotifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ - (void)unregisterOperationFinishedListener:(id<BIOperationNotifierListener>)lis
[self.operationFinishedListeners removeObject:listener];
}

- (void)removeAllListeners {
[self.operationFinishedListeners removeAllObjects];
}

#pragma mark - BIOperationBase methods

- (void)safeCallDidFinishWithErrorCallback:(nonnull NSError *)error {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// MockBIOperationNotifierListener.h
// BIObjCHelpers
//
// Created by Bogdan Iusco on 6/22/16.
// Copyright © 2016 iGama Apps. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <BIObjCHelpers/BIObjCHelpers.h>

@interface MockBIOperationNotifierListener : NSObject<BIOperationNotifierListener>


@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// MockBIOperationNotifierListener.m
// BIObjCHelpers
//
// Created by Bogdan Iusco on 6/22/16.
// Copyright © 2016 iGama Apps. All rights reserved.
//

#import "MockBIOperationNotifierListener.h"

@implementation MockBIOperationNotifierListener

@end
100 changes: 100 additions & 0 deletions BIObjCHelpersTests/Tests/Datasource/Base/BIDatasourceBaseTestCase.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// BIDatasourceBaseTestCase.m
// BIObjCHelpers
//
// Created by Bogdan Iusco on 6/22/16.
// Copyright © 2016 iGama Apps. All rights reserved.
//

#import <XCTest/XCTest.h>
#import <BIObjCHelpers/BIObjCHelpers.h>

#import "MockBIOperationNotifierListener.h"
#import "BIOperationNotifier+Tests.h"

@interface BIDatasourceBaseTestCase : XCTestCase

@property (nonatomic, strong, nullable, readwrite) BIDatasourceBase *datasource;

@end

@implementation BIDatasourceBaseTestCase

- (void)setUp {
[super setUp];
self.datasource = [BIDatasourceBase new];
}

#pragma mark - Test init

- (void)test_init {
BIDatasourceBase *datasource = [BIDatasourceBase new];
XCTAssertNotNil(datasource);
XCTAssertNotNil(datasource.operations);
XCTAssertEqual(datasource.operations.count, 0U);
}

#pragma mark - Test cancelAllCurrentOperations

- (void)test_cancelAllCurrentOperations_silent {
// GIVEN: A set of operations
NSInteger countOperations = arc4random_uniform(10);
NSMutableArray<BIOperationNotifier *> *operations = [NSMutableArray new];
__block NSError *returnedError;
for (NSInteger index = 0; index < countOperations; index++) {
BIOperationNotifier *operation = [BIOperationNotifier new];
operation.didFinishWithErrorCallback = ^(NSError *__nonnull error) {
returnedError = error;
};
MockBIOperationNotifierListener *listener = [MockBIOperationNotifierListener new];
[operation registerOperationFinishedListener:listener];
[self.datasource.operations addObject:operation];
[operations addObject:operation];
}
// WHEN: cancelAllCurrentOperations method is called
[self.datasource cancelAllCurrentOperations:YES];
// THEN: Datasource should have no operations
XCTAssertEqual(self.datasource.operations.count, 0);
// THEN: All operations should not have a didFinishWithErrorCallback set
for (BIOperationNotifier *operation in operations) {
XCTAssertNil(operation.didFinishWithErrorCallback);
}
// THEN: All operations should have no listeners
for (BIOperationNotifier *operation in operations) {
XCTAssertEqual(operation.operationFinishedListeners.count, 0);
}

// THEN: Operation's didFinishWithErrorCallback should not have been called
XCTAssertNil(returnedError);
}

- (void)test_cancelAllCurrentOperations_notSilent {
// GIVEN: A set of operations
NSInteger countOperations = arc4random_uniform(10);
NSMutableArray<BIOperationNotifier *> *operations = [NSMutableArray new];
__block NSError *returnedError;
for (NSInteger index = 0; index < countOperations; index++) {
BIOperationNotifier *operation = [BIOperationNotifier new];
operation.didFinishWithErrorCallback = ^(NSError *__nonnull error) {
returnedError = error;
};
MockBIOperationNotifierListener *listener = [MockBIOperationNotifierListener new];
[operation registerOperationFinishedListener:listener];
[self.datasource.operations addObject:operation];
[operations addObject:operation];
}
// WHEN: cancelAllCurrentOperations method is called
[self.datasource cancelAllCurrentOperations:NO];
// THEN: Datasource should have no operations
XCTAssertEqual(self.datasource.operations.count, 0);
// THEN: All operations should have a didFinishWithErrorCallback set
for (BIOperationNotifier *operation in operations) {
XCTAssertNotNil(operation.didFinishWithErrorCallback);
}
// THEN: All operations should have listeners
for (BIOperationNotifier *operation in operations) {
XCTAssertEqual(operation.operationFinishedListeners.count, 1);
}
}

@end

This file was deleted.

15 changes: 15 additions & 0 deletions BIObjCHelpersTests/Tests/Operations/BIOperationNotifier+Tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// BIOperationNotifier+Tests.h
// BIObjCHelpers
//
// Created by Bogdan Iusco on 6/22/16.
// Copyright © 2016 iGama Apps. All rights reserved.
//

#import <BIObjCHelpers/BIObjCHelpers.h>

@interface BIOperationNotifier (Tests)

@property (nonatomic, strong, nullable, readwrite) NSHashTable <id<BIOperationNotifierListener>> *operationFinishedListeners;

@end

0 comments on commit a7d88fc

Please sign in to comment.