-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean Up ASAsyncTransaction #trivial #316
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ | |
@interface ASAsyncTransactionOperation : NSObject | ||
- (instancetype)initWithOperationCompletionBlock:(asyncdisplaykit_async_transaction_operation_completion_block_t)operationCompletionBlock; | ||
@property (nonatomic, copy) asyncdisplaykit_async_transaction_operation_completion_block_t operationCompletionBlock; | ||
@property (nonatomic, strong) id<NSObject> value; // set on bg queue by the operation block | ||
@property (atomic, strong) id value; // set on bg queue by the operation block | ||
@end | ||
|
||
@implementation ASAsyncTransactionOperation | ||
|
@@ -55,16 +55,17 @@ - (void)dealloc | |
|
||
- (void)callAndReleaseCompletionBlock:(BOOL)canceled; | ||
{ | ||
ASDisplayNodeAssertMainThread(); | ||
if (_operationCompletionBlock) { | ||
_operationCompletionBlock(self.value, canceled); | ||
// Guarantee that _operationCompletionBlock is released on _callbackQueue: | ||
self.operationCompletionBlock = nil; | ||
// Guarantee that _operationCompletionBlock is released on main thread | ||
_operationCompletionBlock = nil; | ||
} | ||
} | ||
|
||
- (NSString *)description | ||
{ | ||
return [NSString stringWithFormat:@"<ASAsyncTransactionOperation: %p - value = %@", self, self.value]; | ||
return [NSString stringWithFormat:@"<ASAsyncTransactionOperation: %p - value = %@>", self, self.value]; | ||
} | ||
|
||
@end | ||
|
@@ -328,27 +329,25 @@ - (NSString *)description | |
return *instance; | ||
} | ||
|
||
@interface _ASAsyncTransaction () | ||
@property (atomic) ASAsyncTransactionState state; | ||
@end | ||
|
||
|
||
@implementation _ASAsyncTransaction | ||
{ | ||
ASAsyncTransactionQueue::Group *_group; | ||
NSMutableArray<ASAsyncTransactionOperation *> *_operations; | ||
_Atomic(ASAsyncTransactionState) _state; | ||
} | ||
|
||
#pragma mark - | ||
#pragma mark Lifecycle | ||
|
||
- (instancetype)initWithCallbackQueue:(dispatch_queue_t)callbackQueue | ||
completionBlock:(void(^)(_ASAsyncTransaction *, BOOL))completionBlock | ||
- (instancetype)initWithCompletionBlock:(void(^)(_ASAsyncTransaction *, BOOL))completionBlock | ||
{ | ||
if ((self = [self init])) { | ||
if (callbackQueue == NULL) { | ||
callbackQueue = dispatch_get_main_queue(); | ||
} | ||
_callbackQueue = callbackQueue; | ||
_completionBlock = completionBlock; | ||
|
||
_state = ATOMIC_VAR_INIT(ASAsyncTransactionStateOpen); | ||
self.state = ASAsyncTransactionStateOpen; | ||
} | ||
return self; | ||
} | ||
|
@@ -362,18 +361,6 @@ - (void)dealloc | |
} | ||
} | ||
|
||
#pragma mark - Properties | ||
|
||
- (ASAsyncTransactionState)state | ||
{ | ||
return atomic_load(&_state); | ||
} | ||
|
||
- (void)setState:(ASAsyncTransactionState)state | ||
{ | ||
atomic_store(&_state, state); | ||
} | ||
|
||
#pragma mark - Transaction Management | ||
|
||
- (void)addAsyncOperationWithBlock:(asyncdisplaykit_async_transaction_async_operation_block_t)block | ||
|
@@ -402,7 +389,7 @@ - (void)addAsyncOperationWithBlock:(asyncdisplaykit_async_transaction_async_oper | |
@autoreleasepool { | ||
if (self.state != ASAsyncTransactionStateCanceled) { | ||
_group->enter(); | ||
block(^(id<NSObject> value){ | ||
block(^(id value){ | ||
operation.value = value; | ||
_group->leave(); | ||
}); | ||
|
@@ -445,7 +432,8 @@ - (void)addOperationWithBlock:(asyncdisplaykit_async_transaction_operation_block | |
- (void)addCompletionBlock:(asyncdisplaykit_async_transaction_completion_block_t)completion | ||
{ | ||
__weak __typeof__(self) weakSelf = self; | ||
[self addOperationWithBlock:^(){return (id<NSObject>)nil;} queue:_callbackQueue completion:^(id<NSObject> value, BOOL canceled) { | ||
dispatch_queue_t bsQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | ||
[self addOperationWithBlock:^(){return (id)nil;} queue:bsQueue completion:^(id value, BOOL canceled) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I bet the (id)nil cast isn't needed anymore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surprisingly it is! The compiler inferred |
||
__typeof__(self) strongSelf = weakSelf; | ||
completion(strongSelf, canceled); | ||
}]; | ||
|
@@ -472,17 +460,15 @@ - (void)commit | |
} else { | ||
NSAssert(_group != NULL, @"If there are operations, dispatch group should have been created"); | ||
|
||
_group->notify(_callbackQueue, ^{ | ||
// _callbackQueue is the main queue in current practice (also asserted in -waitUntilComplete). | ||
// This code should be reviewed before taking on significantly different use cases. | ||
ASAsyncTransactionAssertMainThread(); | ||
_group->notify(dispatch_get_main_queue(), ^{ | ||
[self completeTransaction]; | ||
}); | ||
} | ||
} | ||
|
||
- (void)completeTransaction | ||
{ | ||
ASAsyncTransactionAssertMainThread(); | ||
ASAsyncTransactionState state = self.state; | ||
if (state != ASAsyncTransactionStateComplete) { | ||
BOOL isCanceled = (state == ASAsyncTransactionStateCanceled); | ||
|
@@ -506,7 +492,6 @@ - (void)waitUntilComplete | |
ASAsyncTransactionAssertMainThread(); | ||
if (self.state != ASAsyncTransactionStateComplete) { | ||
if (_group) { | ||
NSAssert(_callbackQueue == dispatch_get_main_queue(), nil); | ||
_group->wait(); | ||
|
||
// At this point, the asynchronous operation may have completed, but the runloop | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wtf does bsQueue mean? lol
Either way, shouldn't this operation be called on the main queue, if _callbackQueue used to be main? This seems like it might be a behavior change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bsQueue
means "bullshit queue." The completion will be called on main still, but the bs "work" that we submitted should be done on another queue. If we try to do the work on the main queue, the transaction won't be safe to wait on (the group will get stuck.)This method isn't currently used and I'd be glad to remove it. Thoughts?