-
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
7 changed files
with
363 additions
and
26 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// BIOperationBase.h | ||
// BIObjCHelpers | ||
// | ||
// Created by Bogdan Iusco on 5/31/16. | ||
// Copyright © 2016 iGama Apps. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
extern void dispatchCodeOnMainThread(void(^__nonnull codeBlock)()); | ||
|
||
@interface BIOperationBase : NSOperation | ||
|
||
// NSOperation | ||
@property (nonatomic, assign, readonly) BOOL isExecuting; | ||
@property (nonatomic, assign, readonly) BOOL isFinished; | ||
|
||
// Callbacks | ||
@property (nonatomic, copy, nullable) void(^didFinishWithErrorCallback)(NSError *__nonnull error); | ||
@property (nonatomic, copy, nullable) void(^didFinishSuccessfullyCallback)(id __nonnull responseObject); | ||
@property (nonatomic, copy, nullable) void(^didFinishCommonCallback)(); | ||
@property (nonatomic, assign) BOOL runCallbacksOnMainThread; | ||
|
||
// Handle operations response states | ||
- (void)handleDidFinishedWithResponse:(nonnull id)response; | ||
- (void)handleDidFinishedWithError:(nonnull NSError *)error; | ||
- (void)handleDidFinishedCommon; | ||
- (void)finish; | ||
|
||
// Trigger callbacks | ||
- (void)safeCallDidFinishWithErrorCallback:(nonnull NSError *)error; | ||
- (void)safeCallDidFinishSuccessfullyCallback:(nonnull id)responseObject; | ||
- (void)safeCallDidFinishCommon; | ||
|
||
@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,116 @@ | ||
// | ||
// BIOperationBase.m | ||
// BIObjCHelpers | ||
// | ||
// Created by Bogdan Iusco on 5/31/16. | ||
// Copyright © 2016 iGama Apps. All rights reserved. | ||
// | ||
|
||
#import "BIOperationBase.h" | ||
|
||
void dispatchCodeOnMainThread(void(^__nonnull codeBlock)()) { | ||
if (![NSThread isMainThread]) { | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
codeBlock(); | ||
}); | ||
} else { | ||
codeBlock(); | ||
} | ||
} | ||
|
||
@implementation BIOperationBase | ||
|
||
#pragma mark - Init methods | ||
|
||
- (nonnull instancetype)init { | ||
self = [super init]; | ||
if (self) { | ||
self.runCallbacksOnMainThread = YES; | ||
} | ||
return self; | ||
} | ||
|
||
#pragma mark - NSOperation methods | ||
|
||
- (void)start { | ||
dispatchCodeOnMainThread( ^{ | ||
[self willChangeValueForKey:@"isExecuting"]; | ||
_isExecuting = YES; | ||
[self didChangeValueForKey:@"isExecuting"]; | ||
}); | ||
} | ||
|
||
#pragma mark - Public methods | ||
|
||
- (void)safeCallDidFinishWithErrorCallback:(nonnull NSError *)error { | ||
void(^block)() = ^{ | ||
if (self.didFinishWithErrorCallback) { | ||
self.didFinishWithErrorCallback(error); | ||
} | ||
[self handleDidFinishedCommon]; | ||
}; | ||
if (self.runCallbacksOnMainThread) { | ||
dispatchCodeOnMainThread( ^{ | ||
block(); | ||
}); | ||
} else { | ||
block(); | ||
} | ||
} | ||
|
||
- (void)safeCallDidFinishSuccessfullyCallback:(nonnull id)responseObject { | ||
void(^block)() = ^{ | ||
if (self.didFinishSuccessfullyCallback) { | ||
self.didFinishSuccessfullyCallback(responseObject); | ||
} | ||
[self handleDidFinishedCommon]; | ||
}; | ||
if (self.runCallbacksOnMainThread) { | ||
dispatchCodeOnMainThread( ^{ | ||
block(); | ||
}); | ||
} else { | ||
block(); | ||
} | ||
} | ||
|
||
- (void)safeCallDidFinishCommon { | ||
void(^block)() = ^{ | ||
if (self.didFinishCommonCallback) { | ||
self.didFinishCommonCallback(); | ||
} | ||
}; | ||
if (self.runCallbacksOnMainThread) { | ||
dispatchCodeOnMainThread( ^{ | ||
block(); | ||
}); | ||
} else { | ||
block(); | ||
} | ||
} | ||
|
||
- (void)handleDidFinishedWithResponse:(id)response { | ||
[self safeCallDidFinishSuccessfullyCallback:response]; | ||
} | ||
|
||
- (void)handleDidFinishedWithError:(NSError *)error { | ||
[self safeCallDidFinishWithErrorCallback:error]; | ||
} | ||
|
||
- (void)handleDidFinishedCommon { | ||
[self safeCallDidFinishCommon]; | ||
[self finish]; | ||
} | ||
|
||
- (void)finish { | ||
dispatchCodeOnMainThread( ^{ | ||
[self willChangeValueForKey:@"isExecuting"]; | ||
[self willChangeValueForKey:@"isFinished"]; | ||
_isExecuting = NO; | ||
_isFinished = YES; | ||
[self didChangeValueForKey:@"isExecuting"]; | ||
[self didChangeValueForKey:@"isFinished"]; | ||
}); | ||
} | ||
|
||
@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
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
Oops, something went wrong.