Skip to content

Commit fd67253

Browse files
authored
DOC-8513 -- Apply objc code feedback (#470)
https://issues.couchbase.com/browse/DOC-8513
1 parent 83dd8b9 commit fd67253

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[]
@@ -1416,7 +1416,7 @@ - (instancetype) init {
14161416
self = [super init];
14171417
if (self) {
14181418
// tag::message-endpoint[]
1419-
CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"dbname" error:nil];
1419+
CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"dbname" error: &error];
14201420

14211421
// The delegate must implement the `CBLMessageEndpointDelegate` protocol.
14221422
NSString* id = @"";
@@ -1515,7 +1515,7 @@ @implementation PassivePeerConnection {
15151515

15161516
- (void)startListener {
15171517
// tag::listener[]
1518-
CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"mydb" error:nil];
1518+
CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"mydb" error: &error];
15191519

15201520
CBLMessageEndpointListenerConfiguration *config =
15211521
[[CBLMessageEndpointListenerConfiguration alloc] initWithDatabase:database
@@ -1586,38 +1586,38 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl
15861586
// QUERY RESULT SET HANDLING EXAMPLES
15871587

15881588
// tag::query-syntax-all[]
1589+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
1590+
15891591
CBLQuery *listQuery;
15901592

15911593
*listQuery = [CBLQueryBuilder select:@[[CBLQuerySelectResult all]]
1592-
from:[CBLQueryDataSource database:dbName] // <.>
1594+
from:[CBLQueryDataSource database:db]] // <.>
15931595

15941596
// end::query-syntax-all[]
15951597

15961598

15971599
// tag::query-access-all[]
1598-
CBLMutableArray* matches = [[CBLMutableArray alloc] init];
1600+
NSMutableArray* matches = [[NSMutableArray alloc] init]; // add to native dictionary
1601+
CBLQueryResultSet* resultset = [listQuery execute:&error];
15991602

1600-
CBLQueryResultSet* resultset = [listQuery execute:&error];
1601-
1602-
for (CBLQueryResult *result in resultset) {
1603+
for (CBLQueryResult *result in resultset.allResults) { // access the resultSet.allResults
16031604

1604-
CBLDictionary *match = [result valueForKey:@dbName];
1605+
CBLDictionary *match = [result valueAtIndex: 0];
16051606

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

1609-
// <.>
1610-
*docid = [match stringForKey:@"id"]
1611-
*name = [match stringForKey:@"name"]
1612-
*type = [match stringForKey:@"type"]
1613-
*city = [match stringForKey:@"city"]
1614-
1615-
} // end for
1609+
NSLog(@"id = %@", [match stringForKey:@"id"]);
1610+
NSLog(@"name = %@", [match stringForKey:@"name"]);
1611+
NSLog(@"type = %@", [match stringForKey:@"type"]);
1612+
NSLog(@"city = %@", [match stringForKey:@"city"]);
1613+
} // end for
16161614

16171615
// end::query-access-all[]
16181616

16191617

16201618
// tag::query-syntax-props[]
1619+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
1620+
16211621
CBLQuery *listQuery;
16221622

16231623
CBLQuerySelectResult *id = [CBLQuerySelectResult expression:[CBLQueryMeta id]];
@@ -1629,41 +1629,38 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl
16291629
CBLQuerySelectResult *city = [CBLQuerySelectResult property:@"city"];
16301630

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

16341634
// end::query-syntax-props[]
16351635

16361636
// tag::query-access-props[]
1637-
CBLDictionary *match;
1637+
NSMutableArray* matches = [[NSMutableArray alloc] init]; // save to native array
16381638

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

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

1643-
for (CBLQueryResult *result in resultset) {
1644-
1645-
*match = [result toDictionary];
1643+
[matches addObject: [result toDictionary]];
16461644

1647-
[matches addDictionary: *match] // <.>
1645+
NSLog(@"id = %@", [result stringForKey:@"id"]);
1646+
NSLog(@"name = %@", [result stringForKey:@"name"]);
1647+
NSLog(@"type = %@", [result stringForKey:@"type"]);
1648+
NSLog(@"city = %@", [result stringForKey:@"city"]);
16481649

1649-
// <.>
1650-
*docid = [match stringForKey:@"id"]
1651-
*name = [match stringForKey:@"name"]
1652-
*type = [match stringForKey:@"type"]
1653-
*city = [match stringForKey:@"city"]
1654-
1655-
} // end for
1650+
} // end for
16561651

16571652
// end::query-access-props[]
16581653

16591654

16601655

16611656
// tag::query-syntax-count-only[]
1657+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
1658+
16621659
CBLQuerySelectResult *count =
16631660
[CBLQuerySelectResult expression:[CBLQueryFunction count: [CBLQueryExpression all]]];
16641661

16651662
*listQuery = [CBLQueryBuilder select:@[count]
1666-
from:[CBLQueryDataSource database:dbName]] // <.>
1663+
from:[CBLQueryDataSource database:db]] // <.>
16671664

16681665
// end::query-syntax-count-only[]
16691666

@@ -1686,12 +1683,14 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl
16861683

16871684

16881685
// tag::query-syntax-id[]
1686+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
1687+
16891688
CBLQuery *listQuery;
16901689

16911690
CBLQuerySelectResult *id = [CBLQuerySelectResult expression:[CBLQueryMeta id]];
16921691

16931692
*listQuery = [CBLQueryBuilder select:@[id]
1694-
from:[CBLQueryDataSource database:dbName]]
1693+
from:[CBLQueryDataSource database:db]]
16951694

16961695
// end::query-syntax-id[]
16971696

@@ -1722,11 +1721,12 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl
17221721
// tag::query-syntax-pagination[]
17231722
int thisOffset = 0;
17241723
int thisLimit = 20;
1724+
CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error];
17251725

17261726
CBLQuery* listQuery =
17271727
[CBLQueryBuilder
17281728
select: @[[CBLQuerySelectResult all]]
1729-
from: [CBLQueryDataSource database: this_Db]
1729+
from: [CBLQueryDataSource database: db]
17301730
limit: [CBLQueryLimit
17311731
limit: [CBLQueryExpression integer: thisLimit]
17321732
offset: [CBLQueryExpression integer: thisOffset]]

0 commit comments

Comments
 (0)