Skip to content

Commit 74252a8

Browse files
committed
DOC-8513 -- Apply objc code feedback (couchbase#470)
https://issues.couchbase.com/browse/DOC-8513 (cherry picked from commit fd67253)
1 parent 96abe8e commit 74252a8

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

modules/objc/examples/code_snippets/SampleCodeTest.m

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ - (void) dontTestBatchOperations {
267267
[doc setValue:@"user" forKey:@"type"];
268268
[doc setValue:[NSString stringWithFormat:@"user %d", i] forKey:@"name"];
269269
[doc setBoolean:NO forKey:@"admin"];
270-
[database saveDocument:doc error:nil];
270+
[database saveDocument:doc error: &error];
271271
}
272272
}];
273273
// end::batch[]
@@ -1430,7 +1430,7 @@ - (instancetype) init {
14301430
self = [super init];
14311431
if (self) {
14321432
// tag::message-endpoint[]
1433-
CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"dbname" error:nil];
1433+
CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"dbname" error: &error];
14341434

14351435
// The delegate must implement the `CBLMessageEndpointDelegate` protocol.
14361436
NSString* id = @"";
@@ -1529,7 +1529,7 @@ @implementation PassivePeerConnection {
15291529

15301530
- (void)startListener {
15311531
// tag::listener[]
1532-
CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"mydb" error:nil];
1532+
CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"mydb" error: &error];
15331533

15341534
CBLMessageEndpointListenerConfiguration *config =
15351535
[[CBLMessageEndpointListenerConfiguration alloc] initWithDatabase:database
@@ -1600,38 +1600,38 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl
16001600
// QUERY RESULT SET HANDLING EXAMPLES
16011601

16021602
// tag::query-syntax-all[]
1603+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
1604+
16031605
CBLQuery *listQuery;
16041606

16051607
*listQuery = [CBLQueryBuilder select:@[[CBLQuerySelectResult all]]
1606-
from:[CBLQueryDataSource database:dbName] // <.>
1608+
from:[CBLQueryDataSource database:db]] // <.>
16071609

16081610
// end::query-syntax-all[]
16091611

16101612

16111613
// tag::query-access-all[]
1612-
CBLMutableArray* matches = [[CBLMutableArray alloc] init];
1614+
NSMutableArray* matches = [[NSMutableArray alloc] init]; // add to native dictionary
1615+
CBLQueryResultSet* resultset = [listQuery execute:&error];
16131616

1614-
CBLQueryResultSet* resultset = [listQuery execute:&error];
1615-
1616-
for (CBLQueryResult *result in resultset) {
1617+
for (CBLQueryResult *result in resultset.allResults) { // access the resultSet.allResults
16171618

1618-
CBLDictionary *match = [result valueForKey:@dbName];
1619+
CBLDictionary *match = [result valueAtIndex: 0];
16191620

1620-
[matches addDictionary: *match] // <.>
1621-
// NSLog(@"document name :: %@", [match stringForKey:@"name"]);
1621+
[matches addObject: [match toDictionary]];
16221622

1623-
// <.>
1624-
*docid = [match stringForKey:@"id"]
1625-
*name = [match stringForKey:@"name"]
1626-
*type = [match stringForKey:@"type"]
1627-
*city = [match stringForKey:@"city"]
1628-
1629-
} // end for
1623+
NSLog(@"id = %@", [match stringForKey:@"id"]);
1624+
NSLog(@"name = %@", [match stringForKey:@"name"]);
1625+
NSLog(@"type = %@", [match stringForKey:@"type"]);
1626+
NSLog(@"city = %@", [match stringForKey:@"city"]);
1627+
} // end for
16301628

16311629
// end::query-access-all[]
16321630

16331631

16341632
// tag::query-syntax-props[]
1633+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
1634+
16351635
CBLQuery *listQuery;
16361636

16371637
CBLQuerySelectResult *id = [CBLQuerySelectResult expression:[CBLQueryMeta id]];
@@ -1643,41 +1643,38 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl
16431643
CBLQuerySelectResult *city = [CBLQuerySelectResult property:@"city"];
16441644

16451645
*listQuery = [CBLQueryBuilder select:@[id, type, name, city]
1646-
from:[CBLQueryDataSource database:dbName]] // <.>
1646+
from:[CBLQueryDataSource database:db]] // <.>
16471647

16481648
// end::query-syntax-props[]
16491649

16501650
// tag::query-access-props[]
1651-
CBLDictionary *match;
1651+
NSMutableArray* matches = [[NSMutableArray alloc] init]; // save to native array
16521652

1653-
CBLMutableArray* matches = [[CBLMutableArray alloc] init];
1653+
CBLQueryResultSet* resultset = [listQuery execute:&error];
16541654

1655-
CBLQueryResultSet* resultset = [listQuery execute:&error];
1655+
for (CBLQueryResult *result in resultset.allResults) { // all results
16561656

1657-
for (CBLQueryResult *result in resultset) {
1658-
1659-
*match = [result toDictionary];
1657+
[matches addObject: [result toDictionary]];
16601658

1661-
[matches addDictionary: *match] // <.>
1659+
NSLog(@"id = %@", [result stringForKey:@"id"]);
1660+
NSLog(@"name = %@", [result stringForKey:@"name"]);
1661+
NSLog(@"type = %@", [result stringForKey:@"type"]);
1662+
NSLog(@"city = %@", [result stringForKey:@"city"]);
16621663

1663-
// <.>
1664-
*docid = [match stringForKey:@"id"]
1665-
*name = [match stringForKey:@"name"]
1666-
*type = [match stringForKey:@"type"]
1667-
*city = [match stringForKey:@"city"]
1668-
1669-
} // end for
1664+
} // end for
16701665

16711666
// end::query-access-props[]
16721667

16731668

16741669

16751670
// tag::query-syntax-count-only[]
1671+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
1672+
16761673
CBLQuerySelectResult *count =
16771674
[CBLQuerySelectResult expression:[CBLQueryFunction count: [CBLQueryExpression all]]];
16781675

16791676
*listQuery = [CBLQueryBuilder select:@[count]
1680-
from:[CBLQueryDataSource database:dbName]] // <.>
1677+
from:[CBLQueryDataSource database:db]] // <.>
16811678

16821679
// end::query-syntax-count-only[]
16831680

@@ -1700,12 +1697,14 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl
17001697

17011698

17021699
// tag::query-syntax-id[]
1700+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
1701+
17031702
CBLQuery *listQuery;
17041703

17051704
CBLQuerySelectResult *id = [CBLQuerySelectResult expression:[CBLQueryMeta id]];
17061705

17071706
*listQuery = [CBLQueryBuilder select:@[id]
1708-
from:[CBLQueryDataSource database:dbName]]
1707+
from:[CBLQueryDataSource database:db]]
17091708

17101709
// end::query-syntax-id[]
17111710

@@ -1736,11 +1735,12 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl
17361735
// tag::query-syntax-pagination[]
17371736
int thisOffset = 0;
17381737
int thisLimit = 20;
1738+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
17391739

17401740
CBLQuery* listQuery =
17411741
[CBLQueryBuilder
17421742
select: @[[CBLQuerySelectResult all]]
1743-
from: [CBLQueryDataSource database: this_Db]
1743+
from: [CBLQueryDataSource database: db]
17441744
limit: [CBLQueryLimit
17451745
limit: [CBLQueryExpression integer: thisLimit]
17461746
offset: [CBLQueryExpression integer: thisOffset]]

0 commit comments

Comments
 (0)