Skip to content

Commit b120406

Browse files
committed
Syntax change to make writing code for YapDatabase more readable. Also a nod to Swift and Trailing Closures.
1 parent ad4e173 commit b120406

File tree

4 files changed

+295
-25
lines changed

4 files changed

+295
-25
lines changed

YapDatabase/YapDatabase.h

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ extern NSString *const YapDatabaseAllKeysRemovedKey;
414414
* NO if an error occurred, such as the extensionName is already registered.
415415
*
416416
* @see asyncRegisterExtension:withName:completionBlock:
417-
* @see asyncRegisterExtension:withName:completionBlock:completionQueue:
417+
* @see asyncRegisterExtension:withName:completionQueue:completionBlock:
418418
**/
419419
- (BOOL)registerExtension:(YapDatabaseExtension *)extension withName:(NSString *)extensionName;
420420

@@ -449,10 +449,43 @@ extern NSString *const YapDatabaseAllKeysRemovedKey;
449449
* Additionally the dispatch_queue to invoke the completion block may also be specified.
450450
* If NULL, dispatch_get_main_queue() is automatically used.
451451
**/
452+
- (void)asyncRegisterExtension:(YapDatabaseExtension *)extension
453+
withName:(NSString *)extensionName
454+
completionQueue:(dispatch_queue_t)completionQueue
455+
completionBlock:(void(^)(BOOL ready))completionBlock;
456+
457+
/**
458+
* DEPRECATED in v2.5
459+
*
460+
* The syntax has been changed in order to make the code easier to read.
461+
* In the past the code would end up looking like this:
462+
*
463+
* [database asyncRegisterExtension:ext
464+
* withName:@"name"
465+
* completionBlock:^
466+
* {
467+
* // A bunch of code here
468+
* // code...
469+
* // code...
470+
* } completionQueue:importantQueue]; <-- Hidden in code. Often overlooked.
471+
*
472+
* The new syntax puts the completionQueue declaration before the completionBlock declaration.
473+
* Since the two are intricately linked, they should be next to each other in code.
474+
* Then end result is easier to read:
475+
*
476+
* [database asyncRegisterExtension:ext
477+
* withName:@"name"
478+
* completionQueue:importantQueue <-- Easier to see
479+
* completionBlock:^
480+
* {
481+
* // 100 lines of code here
482+
* }];
483+
**/
452484
- (void)asyncRegisterExtension:(YapDatabaseExtension *)extension
453485
withName:(NSString *)extensionName
454486
completionBlock:(void(^)(BOOL ready))completionBlock
455-
completionQueue:(dispatch_queue_t)completionQueue;
487+
completionQueue:(dispatch_queue_t)completionQueue
488+
__attribute((deprecated("Use method asyncRegisterExtension:withName:completionQueue:completionBlock: instead")));
456489

457490
/**
458491
* This method unregisters an extension with the given name.
@@ -475,7 +508,7 @@ extern NSString *const YapDatabaseAllKeysRemovedKey;
475508
* And it will automatically unregister these orhpaned extensions for you.
476509
*
477510
* @see asyncUnregisterExtensionWithName:completionBlock:
478-
* @see asyncUnregisterExtensionWithName:completionBlock:completionQueue:
511+
* @see asyncUnregisterExtensionWithName:completionQueue:completionBlock:
479512
**/
480513
- (void)unregisterExtensionWithName:(NSString *)extensionName;
481514

@@ -506,9 +539,38 @@ extern NSString *const YapDatabaseAllKeysRemovedKey;
506539
* If NULL, dispatch_get_main_queue() is automatically used.
507540
**/
508541
- (void)asyncUnregisterExtensionWithName:(NSString *)extensionName
509-
completionBlock:(dispatch_block_t)completionBlock
510-
completionQueue:(dispatch_queue_t)completionQueue;
542+
completionQueue:(dispatch_queue_t)completionQueue
543+
completionBlock:(dispatch_block_t)completionBlock;
511544

545+
/**
546+
* DEPRECATED in v2.5
547+
*
548+
* The syntax has been changed in order to make the code easier to read.
549+
* In the past the code would end up looking like this:
550+
*
551+
* [database asyncUnregisterExtensionWithName:@"name"
552+
* completionBlock:^
553+
* {
554+
* // A bunch of code here
555+
* // code...
556+
* // code...
557+
* } completionQueue:importantQueue]; <-- Hidden in code. Often overlooked.
558+
*
559+
* The new syntax puts the completionQueue declaration before the completionBlock declaration.
560+
* Since the two are intricately linked, they should be next to each other in code.
561+
* Then end result is easier to read:
562+
*
563+
* [database asyncUnregisterExtensionWithName:@"name"
564+
* completionQueue:importantQueue <-- Easier to see
565+
* completionBlock:^
566+
* {
567+
* // 100 lines of code here
568+
* }];
569+
**/
570+
- (void)asyncUnregisterExtensionWithName:(NSString *)extensionName
571+
completionBlock:(dispatch_block_t)completionBlock
572+
completionQueue:(dispatch_queue_t)completionQueue
573+
__attribute((deprecated("Use method asyncUnregisterExtensionWithName:completionQueue:completionBlock: instead")));
512574

513575
/**
514576
* DEPRECATED in v2.5
@@ -523,7 +585,7 @@ __attribute((deprecated("Use method asyncUnregisterExtensionWithName:completionB
523585
- (void)asyncUnregisterExtension:(NSString *)extensionName
524586
completionBlock:(dispatch_block_t)completionBlock
525587
completionQueue:(dispatch_queue_t)completionQueue
526-
__attribute((deprecated("Use method asyncUnregisterExtensionWithName:completionBlock:completionQueue: instead")));
588+
__attribute((deprecated("Use method asyncUnregisterExtensionWithName:completionQueue:completionBlock: instead")));
527589

528590
/**
529591
* Returns the registered extension with the given name.

YapDatabase/YapDatabase.m

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,7 @@ - (YapDatabaseConnection *)newConnection
13301330
* NO if an error occurred, such as the extensionName is already registered.
13311331
*
13321332
* @see asyncRegisterExtension:withName:completionBlock:
1333-
* @see asyncRegisterExtension:withName:completionBlock:completionQueue:
1333+
* @see asyncRegisterExtension:withName:completionQueue:completionBlock:
13341334
**/
13351335
- (BOOL)registerExtension:(YapDatabaseExtension *)extension withName:(NSString *)extensionName
13361336
{
@@ -1363,8 +1363,8 @@ - (void)asyncRegisterExtension:(YapDatabaseExtension *)extension
13631363
{
13641364
[self asyncRegisterExtension:extension
13651365
withName:extensionName
1366-
completionBlock:completionBlock
1367-
completionQueue:NULL];
1366+
completionQueue:NULL
1367+
completionBlock:completionBlock];
13681368
}
13691369

13701370
/**
@@ -1383,8 +1383,8 @@ - (void)asyncRegisterExtension:(YapDatabaseExtension *)extension
13831383
**/
13841384
- (void)asyncRegisterExtension:(YapDatabaseExtension *)extension
13851385
withName:(NSString *)extensionName
1386-
completionBlock:(void(^)(BOOL ready))completionBlock
13871386
completionQueue:(dispatch_queue_t)completionQueue
1387+
completionBlock:(void(^)(BOOL ready))completionBlock
13881388
{
13891389
if (completionQueue == NULL && completionBlock != NULL)
13901390
completionQueue = dispatch_get_main_queue();
@@ -1403,6 +1403,44 @@ - (void)asyncRegisterExtension:(YapDatabaseExtension *)extension
14031403
}});
14041404
}
14051405

1406+
/**
1407+
* DEPRECATED in v2.5
1408+
*
1409+
* The syntax has been changed in order to make the code easier to read.
1410+
* In the past the code would end up looking like this:
1411+
*
1412+
* [database asyncRegisterExtension:ext
1413+
* withName:@"name"
1414+
* completionBlock:^
1415+
* {
1416+
* // A bunch of code here
1417+
* // code...
1418+
* // code...
1419+
* } completionQueue:importantQueue]; <-- Hidden in code. Often overlooked.
1420+
*
1421+
* The new syntax puts the completionQueue declaration before the completionBlock declaration.
1422+
* Since the two are intricately linked, they should be next to each other in code.
1423+
* Then end result is easier to read:
1424+
*
1425+
* [database asyncRegisterExtension:ext
1426+
* withName:@"name"
1427+
* completionQueue:importantQueue <-- Easier to see
1428+
* completionBlock:^
1429+
* {
1430+
* // 100 lines of code here
1431+
* }];
1432+
**/
1433+
- (void)asyncRegisterExtension:(YapDatabaseExtension *)extension
1434+
withName:(NSString *)extensionName
1435+
completionBlock:(void(^)(BOOL ready))completionBlock
1436+
completionQueue:(dispatch_queue_t)completionQueue
1437+
{
1438+
[self asyncRegisterExtension:extension
1439+
withName:extensionName
1440+
completionQueue:completionQueue
1441+
completionBlock:completionBlock];
1442+
}
1443+
14061444
/**
14071445
* This method unregisters an extension with the given name.
14081446
* The associated underlying tables will be dropped from the database.
@@ -1424,7 +1462,7 @@ - (void)asyncRegisterExtension:(YapDatabaseExtension *)extension
14241462
* And it will automatically unregister these orhpaned extensions for you.
14251463
*
14261464
* @see asyncUnregisterExtensionWithName:completionBlock:
1427-
* @see asyncUnregisterExtensionWithName:completionBlock:completionQueue:
1465+
* @see asyncUnregisterExtensionWithName:completionQueue:completionBlock:
14281466
**/
14291467
- (void)unregisterExtensionWithName:(NSString *)extensionName
14301468
{
@@ -1449,13 +1487,25 @@ - (void)asyncUnregisterExtensionWithName:(NSString *)extensionName
14491487
completionBlock:(dispatch_block_t)completionBlock
14501488
{
14511489
[self asyncUnregisterExtensionWithName:extensionName
1452-
completionBlock:completionBlock
1453-
completionQueue:NULL];
1490+
completionQueue:NULL
1491+
completionBlock:completionBlock];
14541492
}
14551493

1494+
/**
1495+
* Asynchronoulsy starts the extension unregistration process.
1496+
*
1497+
* The unregistration process is equivalent to a readwrite transaction.
1498+
* It involves deleting various information about the extension from the database,
1499+
* as well as possibly dropping related tables the extension may have been using.
1500+
*
1501+
* An optional completion block may be used.
1502+
*
1503+
* Additionally the dispatch_queue to invoke the completion block may also be specified.
1504+
* If NULL, dispatch_get_main_queue() is automatically used.
1505+
**/
14561506
- (void)asyncUnregisterExtensionWithName:(NSString *)extensionName
1457-
completionBlock:(dispatch_block_t)completionBlock
14581507
completionQueue:(dispatch_queue_t)completionQueue
1508+
completionBlock:(dispatch_block_t)completionBlock
14591509
{
14601510
if (completionQueue == NULL && completionBlock != NULL)
14611511
completionQueue = dispatch_get_main_queue();
@@ -1474,6 +1524,40 @@ - (void)asyncUnregisterExtensionWithName:(NSString *)extensionName
14741524
}});
14751525
}
14761526

1527+
/**
1528+
* DEPRECATED in v2.5
1529+
*
1530+
* The syntax has been changed in order to make the code easier to read.
1531+
* In the past the code would end up looking like this:
1532+
*
1533+
* [database asyncUnregisterExtensionWithName:@"name"
1534+
* completionBlock:^
1535+
* {
1536+
* // A bunch of code here
1537+
* // code...
1538+
* // code...
1539+
* } completionQueue:importantQueue]; <-- Hidden in code. Often overlooked.
1540+
*
1541+
* The new syntax puts the completionQueue declaration before the completionBlock declaration.
1542+
* Since the two are intricately linked, they should be next to each other in code.
1543+
* Then end result is easier to read:
1544+
*
1545+
* [database asyncUnregisterExtensionWithName:@"name"
1546+
* completionQueue:importantQueue <-- Easier to see
1547+
* completionBlock:^
1548+
* {
1549+
* // 100 lines of code here
1550+
* }];
1551+
**/
1552+
- (void)asyncUnregisterExtensionWithName:(NSString *)extensionName
1553+
completionBlock:(dispatch_block_t)completionBlock
1554+
completionQueue:(dispatch_queue_t)completionQueue
1555+
{
1556+
[self asyncUnregisterExtensionWithName:extensionName
1557+
completionQueue:completionQueue
1558+
completionBlock:completionBlock];
1559+
}
1560+
14771561
/**
14781562
* Internal utility method.
14791563
* Handles lazy creation and destruction of short-lived registrationConnection instance.
@@ -1592,15 +1676,15 @@ - (void)asyncUnregisterExtension:(NSString *)extensionName
15921676
/**
15931677
* DEPRECATED in v2.5
15941678
*
1595-
* Use asyncUnregisterExtensionWithName:completionBlock:completionQueue: instead.
1679+
* Use asyncUnregisterExtensionWithName:completionQueue:completionBlock: instead.
15961680
**/
15971681
- (void)asyncUnregisterExtension:(NSString *)extensionName
15981682
completionBlock:(dispatch_block_t)completionBlock
15991683
completionQueue:(dispatch_queue_t)completionQueue
16001684
{
16011685
[self asyncUnregisterExtensionWithName:extensionName
1602-
completionBlock:completionBlock
1603-
completionQueue:completionQueue];
1686+
completionQueue:completionQueue
1687+
completionBlock:completionBlock];
16041688
}
16051689

16061690
/**

YapDatabase/YapDatabaseConnection.h

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,39 @@ typedef NS_ENUM(NSInteger, YapDatabasePolicy) {
257257
* Additionally the dispatch_queue to invoke the completion block may also be specified.
258258
* If NULL, dispatch_get_main_queue() is automatically used.
259259
**/
260+
- (void)asyncReadWithBlock:(void (^)(YapDatabaseReadTransaction *transaction))block
261+
completionQueue:(dispatch_queue_t)completionQueue
262+
completionBlock:(dispatch_block_t)completionBlock;
263+
264+
/**
265+
* DEPRECATED in v2.5
266+
*
267+
* The syntax has been changed in order to make the code easier to read.
268+
* In the past the code would end up looking like this:
269+
*
270+
* [databaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction){
271+
* // 100 lines of code here
272+
* } completionBlock:^{
273+
* // 50 lines of code here
274+
* }
275+
* completionQueue:importantQueue]; <-- Very hidden in code. Often overlooked.
276+
*
277+
* The new syntax puts the completionQueue declaration before the completionBlock declaration.
278+
* Since the two are intricately linked, they should be next to each other in code.
279+
* Then end result is much easier to read:
280+
*
281+
* [databaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction){
282+
* // 100 lines of code here
283+
* }
284+
* completionQueue:importantQueue <-- Easier to see
285+
* completionBlock:^{
286+
* // 50 lines of code here
287+
* }];
288+
**/
260289
- (void)asyncReadWithBlock:(void (^)(YapDatabaseReadTransaction *transaction))block
261290
completionBlock:(dispatch_block_t)completionBlock
262-
completionQueue:(dispatch_queue_t)completionQueue;
291+
completionQueue:(dispatch_queue_t)completionQueue
292+
__attribute((deprecated("Use method asyncReadWithBlock:completionQueue:completionBlock: instead")));
263293

264294
/**
265295
* Read-write access to the database.
@@ -299,9 +329,39 @@ typedef NS_ENUM(NSInteger, YapDatabasePolicy) {
299329
* Additionally the dispatch_queue to invoke the completion block may also be specified.
300330
* If NULL, dispatch_get_main_queue() is automatically used.
301331
**/
332+
- (void)asyncReadWriteWithBlock:(void (^)(YapDatabaseReadWriteTransaction *transaction))block
333+
completionQueue:(dispatch_queue_t)completionQueue
334+
completionBlock:(dispatch_block_t)completionBlock;
335+
336+
/**
337+
* DEPRECATED in v2.5
338+
*
339+
* The syntax has been changed in order to make the code easier to read.
340+
* In the past the code would end up looking like this:
341+
*
342+
* [databaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction){
343+
* // 100 lines of code here
344+
* } completionBlock:^{
345+
* // 50 lines of code here
346+
* }
347+
* completionQueue:importantQueue]; <-- Very hidden in code. Often overlooked.
348+
*
349+
* The new syntax puts the completionQueue declaration before the completionBlock declaration.
350+
* Since the two are intricately linked, they should be next to each other in code.
351+
* Then end result is much easier to read:
352+
*
353+
* [databaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction){
354+
* // 100 lines of code here
355+
* }
356+
* completionQueue:importantQueue <-- Easier to see
357+
* completionBlock:^{
358+
* // 50 lines of code here
359+
* }];
360+
**/
302361
- (void)asyncReadWriteWithBlock:(void (^)(YapDatabaseReadWriteTransaction *transaction))block
303362
completionBlock:(dispatch_block_t)completionBlock
304-
completionQueue:(dispatch_queue_t)completionQueue;
363+
completionQueue:(dispatch_queue_t)completionQueue
364+
__attribute((deprecated("Use method asyncReadWriteWithBlock:completionQueue:completionBlock: instead")));
305365

306366
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
307367
#pragma mark Long-Lived Transactions

0 commit comments

Comments
 (0)