New method: [YapDatabaseReadWriteTransaction addCompletionQueue:completionBlock:]
This commit is contained in:
parent
8e455fe858
commit
60d9471c7f
|
@ -457,6 +457,9 @@ static NSString *const ext_key_class = @"class";
|
|||
|
||||
@interface YapDatabaseReadWriteTransaction () {
|
||||
@public
|
||||
NSMutableArray<dispatch_queue_t> *completionQueueStack;
|
||||
NSMutableArray<dispatch_block_t> *completionBlockStack;
|
||||
|
||||
BOOL rollback;
|
||||
id customObjectForNotification;
|
||||
}
|
||||
|
|
|
@ -1963,6 +1963,18 @@ static int connectionBusyHandler(void *ptr, int count)
|
|||
block(transaction);
|
||||
[self postReadWriteTransaction:transaction];
|
||||
|
||||
if (transaction->completionBlockStack)
|
||||
{
|
||||
NSUInteger count = transaction->completionBlockStack.count;
|
||||
for (NSUInteger i = 0; i < count; i++)
|
||||
{
|
||||
dispatch_queue_t stackItemQueue = transaction->completionQueueStack[i];
|
||||
dispatch_block_t stackItemBlock = transaction->completionBlockStack[i];
|
||||
|
||||
dispatch_async(stackItemQueue, stackItemBlock);
|
||||
}
|
||||
}
|
||||
|
||||
}}); // End dispatch_sync(database->writeQueue)
|
||||
|
||||
}); // End dispatch_sync(connectionQueue)
|
||||
|
@ -2144,6 +2156,18 @@ static int connectionBusyHandler(void *ptr, int count)
|
|||
block(transaction);
|
||||
[self postReadWriteTransaction:transaction];
|
||||
|
||||
if (transaction->completionBlockStack)
|
||||
{
|
||||
NSUInteger count = transaction->completionBlockStack.count;
|
||||
for (NSUInteger i = 0; i < count; i++)
|
||||
{
|
||||
dispatch_queue_t stackItemQueue = transaction->completionQueueStack[i];
|
||||
dispatch_block_t stackItemBlock = transaction->completionBlockStack[i];
|
||||
|
||||
dispatch_async(stackItemQueue, stackItemBlock);
|
||||
}
|
||||
}
|
||||
|
||||
if (completionBlock)
|
||||
dispatch_async(completionQueue, completionBlock);
|
||||
|
||||
|
|
|
@ -783,6 +783,21 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
**/
|
||||
- (void)removeAllObjectsInAllCollections;
|
||||
|
||||
#pragma mark Completion
|
||||
|
||||
/**
|
||||
* It's often useful to compose code into various reusable functions which take a
|
||||
* YapDatabaseReadWriteTransaction as a parameter. However, the ability to compose code
|
||||
* in this manner is often prevented by the need to perform a task after the commit has finished.
|
||||
*
|
||||
* The end result is that programmers either end up copy-pasting code,
|
||||
* or hack together a solution that involves functions returning completion blocks.
|
||||
*
|
||||
* This method solves the dilemma by allowing encapsulated code to register its own commit completionBlock.
|
||||
**/
|
||||
- (void)addCompletionQueue:(nullable dispatch_queue_t)completionQueue
|
||||
completionBlock:(dispatch_block_t)completionBlock;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
|
|
@ -5977,6 +5977,39 @@
|
|||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma mark Completion
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* It's often useful to compose code into various reusable functions which take a
|
||||
* YapDatabaseReadWriteTransaction as a parameter. However, the ability to compose code
|
||||
* in this manner is often prevented by the need to perform a task after the commit has finished.
|
||||
*
|
||||
* The end result is that programmers either end up copy-pasting code,
|
||||
* or hack together a solution that involves functions returning completion blocks.
|
||||
*
|
||||
* This method solves the dilemma by allowing encapsulated code to register its own commit completionBlock.
|
||||
**/
|
||||
- (void)addCompletionQueue:(dispatch_queue_t)completionQueue
|
||||
completionBlock:(dispatch_block_t)completionBlock
|
||||
{
|
||||
if (completionBlock == nil)
|
||||
return;
|
||||
|
||||
if (completionQueue == nil)
|
||||
completionQueue = dispatch_get_main_queue();
|
||||
|
||||
if (completionQueueStack == nil)
|
||||
completionQueueStack = [[NSMutableArray alloc] initWithCapacity:1];
|
||||
|
||||
if (completionBlockStack == nil)
|
||||
completionBlockStack = [[NSMutableArray alloc] initWithCapacity:1];
|
||||
|
||||
[completionQueueStack addObject:completionQueue];
|
||||
[completionBlockStack addObject:completionBlock];
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma mark Extensions
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Reference in New Issue