-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// BIOperationNotifier.h | ||
// BIObjCHelpers | ||
// | ||
// Created by Bogdan Iusco on 5/11/16. | ||
// Copyright © 2016 iGama Apps. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@class BIOperationNotifier; | ||
|
||
@protocol BIOperationNotifierListener <NSObject> | ||
|
||
@optional | ||
- (void)operation:(BIOperationNotifier *__nonnull)operation didFinishWithResponse:(id __nullable)response; | ||
- (void)operation:(BIOperationNotifier *__nonnull)operation didFinishWithError:(NSError *__nullable)error; | ||
- (void)operationDidFinishCommon:(BIOperationNotifier *__nonnull)operation; | ||
|
||
@end | ||
|
||
@interface BIOperationNotifier : NSOperation | ||
|
||
@property (nonatomic, copy, nullable) void(^didFinishWithErrorCallback)(NSError *__nonnull error); | ||
@property (nonatomic, copy, nullable) void(^didFinishSuccessfullyCallback)(id __nonnull responseObject); | ||
@property (nonatomic, copy, nullable) void(^didFinishCommonCallback)(); | ||
|
||
- (void)registerOperationFinishedListener:(id<BIOperationNotifierListener> __nonnull)listener; | ||
- (void)unregisterOperationFinishedListener:(id<BIOperationNotifierListener> __nonnull)listener; | ||
|
||
- (void)safeCallDidFinishWithErrorCallback:(nonnull NSError *)error; | ||
- (void)safeCallDidFinishSuccessfullyCallback:(nonnull id)responseObject; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// BIOperationNotifier.m | ||
// BIObjCHelpers | ||
// | ||
// Created by Bogdan Iusco on 5/11/16. | ||
// Copyright © 2016 iGama Apps. All rights reserved. | ||
// | ||
|
||
#import "BIOperationNotifier.h" | ||
|
||
@interface BIOperationNotifier() | ||
|
||
@property (nonatomic, strong, nullable, readwrite) NSHashTable <id<BIOperationNotifierListener>> *operationFinishedListeners; | ||
|
||
@end | ||
|
||
@implementation BIOperationNotifier | ||
|
||
- (void)safeCallDidFinishWithErrorCallback:(nonnull NSError *)error { | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
[self BI_notifyOperationDidFinishWithError:error]; | ||
[self safeCallDidFinishCommon]; | ||
}); | ||
} | ||
|
||
- (void)safeCallDidFinishSuccessfullyCallback:(nonnull id)responseObject { | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
[self BI_notifyOperationDidFinishWithResponse:responseObject]; | ||
[self safeCallDidFinishCommon]; | ||
}); | ||
} | ||
|
||
- (void)safeCallDidFinishCommon { | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
[self BI_notifyOperationDidFinishCommon]; | ||
}); | ||
} | ||
|
||
#pragma mark - Public methods | ||
|
||
- (void)registerOperationFinishedListener:(id<BIOperationNotifierListener>)listener { | ||
[self.operationFinishedListeners addObject:listener]; | ||
} | ||
|
||
- (void)unregisterOperationFinishedListener:(id<BIOperationNotifierListener>)listener { | ||
[self.operationFinishedListeners removeObject:listener]; | ||
} | ||
|
||
#pragma mark - Properties methods | ||
|
||
- (NSHashTable<id<BIOperationNotifierListener>> *)operationFinishedListeners { | ||
if (!_operationFinishedListeners) { | ||
_operationFinishedListeners = [NSHashTable weakObjectsHashTable]; | ||
} | ||
return _operationFinishedListeners; | ||
} | ||
|
||
#pragma mark - Private methods | ||
|
||
- (void)BI_notifyOperationDidFinishWithResponse:(id __nullable)response { | ||
if (self.didFinishSuccessfullyCallback) { | ||
self.didFinishSuccessfullyCallback(response); | ||
} | ||
NSHashTable *copiedListeners = [self.operationFinishedListeners copy]; | ||
for (id<BIOperationNotifierListener> listener in copiedListeners) { | ||
if ([listener respondsToSelector:@selector(operation:didFinishWithResponse:)]) { | ||
[listener operation:self didFinishWithResponse:response]; | ||
} | ||
} | ||
} | ||
|
||
- (void)BI_notifyOperationDidFinishWithError:(NSError *__nullable)error { | ||
if (self.didFinishWithErrorCallback) { | ||
self.didFinishWithErrorCallback(error); | ||
} | ||
NSHashTable *copiedListeners = [self.operationFinishedListeners copy]; | ||
for (id<BIOperationNotifierListener> listener in copiedListeners) { | ||
if ([listener respondsToSelector:@selector(operation:didFinishWithError:)]) { | ||
[listener operation:self didFinishWithError:error]; | ||
} | ||
} | ||
} | ||
|
||
- (void)BI_notifyOperationDidFinishCommon { | ||
if (self.didFinishCommonCallback) { | ||
self.didFinishCommonCallback(); | ||
} | ||
NSHashTable *copiedListeners = [self.operationFinishedListeners copy]; | ||
for (id<BIOperationNotifierListener> listener in copiedListeners) { | ||
if ([listener respondsToSelector:@selector(operationDidFinishCommon:)]) { | ||
[listener operationDidFinishCommon:self]; | ||
} | ||
} | ||
} | ||
|
||
@end |