From fd67253213a868c65b403e550e43342709f60d19 Mon Sep 17 00:00:00 2001 From: ibsoln <52778946+ibsoln@users.noreply.github.com> Date: Wed, 2 Jun 2021 10:55:59 +0100 Subject: [PATCH] DOC-8513 -- Apply objc code feedback (#470) https://issues.couchbase.com/browse/DOC-8513 --- .../examples/code_snippets/SampleCodeTest.m | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/modules/objc/examples/code_snippets/SampleCodeTest.m b/modules/objc/examples/code_snippets/SampleCodeTest.m index 7d690991c..d907140f9 100644 --- a/modules/objc/examples/code_snippets/SampleCodeTest.m +++ b/modules/objc/examples/code_snippets/SampleCodeTest.m @@ -267,7 +267,7 @@ - (void) dontTestBatchOperations { [doc setValue:@"user" forKey:@"type"]; [doc setValue:[NSString stringWithFormat:@"user %d", i] forKey:@"name"]; [doc setBoolean:NO forKey:@"admin"]; - [database saveDocument:doc error:nil]; + [database saveDocument:doc error: &error]; } }]; // end::batch[] @@ -1416,7 +1416,7 @@ - (instancetype) init { self = [super init]; if (self) { // tag::message-endpoint[] - CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"dbname" error:nil]; + CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"dbname" error: &error]; // The delegate must implement the `CBLMessageEndpointDelegate` protocol. NSString* id = @""; @@ -1515,7 +1515,7 @@ @implementation PassivePeerConnection { - (void)startListener { // tag::listener[] - CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"mydb" error:nil]; + CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"mydb" error: &error]; CBLMessageEndpointListenerConfiguration *config = [[CBLMessageEndpointListenerConfiguration alloc] initWithDatabase:database @@ -1586,38 +1586,38 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl // QUERY RESULT SET HANDLING EXAMPLES // tag::query-syntax-all[] +CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error]; + CBLQuery *listQuery; *listQuery = [CBLQueryBuilder select:@[[CBLQuerySelectResult all]] - from:[CBLQueryDataSource database:dbName] // <.> + from:[CBLQueryDataSource database:db]] // <.> // end::query-syntax-all[] // tag::query-access-all[] -CBLMutableArray* matches = [[CBLMutableArray alloc] init]; +NSMutableArray* matches = [[NSMutableArray alloc] init]; // add to native dictionary + CBLQueryResultSet* resultset = [listQuery execute:&error]; -CBLQueryResultSet* resultset = [listQuery execute:&error]; - -for (CBLQueryResult *result in resultset) { + for (CBLQueryResult *result in resultset.allResults) { // access the resultSet.allResults - CBLDictionary *match = [result valueForKey:@dbName]; + CBLDictionary *match = [result valueAtIndex: 0]; - [matches addDictionary: *match] // <.> - // NSLog(@"document name :: %@", [match stringForKey:@"name"]); + [matches addObject: [match toDictionary]]; -// <.> - *docid = [match stringForKey:@"id"] - *name = [match stringForKey:@"name"] - *type = [match stringForKey:@"type"] - *city = [match stringForKey:@"city"] - -} // end for + NSLog(@"id = %@", [match stringForKey:@"id"]); + NSLog(@"name = %@", [match stringForKey:@"name"]); + NSLog(@"type = %@", [match stringForKey:@"type"]); + NSLog(@"city = %@", [match stringForKey:@"city"]); + } // end for // end::query-access-all[] // tag::query-syntax-props[] +CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error]; + CBLQuery *listQuery; CBLQuerySelectResult *id = [CBLQuerySelectResult expression:[CBLQueryMeta id]]; @@ -1629,41 +1629,38 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl CBLQuerySelectResult *city = [CBLQuerySelectResult property:@"city"]; *listQuery = [CBLQueryBuilder select:@[id, type, name, city] - from:[CBLQueryDataSource database:dbName]] // <.> + from:[CBLQueryDataSource database:db]] // <.> // end::query-syntax-props[] // tag::query-access-props[] -CBLDictionary *match; + NSMutableArray* matches = [[NSMutableArray alloc] init]; // save to native array -CBLMutableArray* matches = [[CBLMutableArray alloc] init]; + CBLQueryResultSet* resultset = [listQuery execute:&error]; -CBLQueryResultSet* resultset = [listQuery execute:&error]; + for (CBLQueryResult *result in resultset.allResults) { // all results -for (CBLQueryResult *result in resultset) { - - *match = [result toDictionary]; + [matches addObject: [result toDictionary]]; - [matches addDictionary: *match] // <.> + NSLog(@"id = %@", [result stringForKey:@"id"]); + NSLog(@"name = %@", [result stringForKey:@"name"]); + NSLog(@"type = %@", [result stringForKey:@"type"]); + NSLog(@"city = %@", [result stringForKey:@"city"]); -// <.> - *docid = [match stringForKey:@"id"] - *name = [match stringForKey:@"name"] - *type = [match stringForKey:@"type"] - *city = [match stringForKey:@"city"] - -} // end for + } // end for // end::query-access-props[] // tag::query-syntax-count-only[] +CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error]; + CBLQuerySelectResult *count = [CBLQuerySelectResult expression:[CBLQueryFunction count: [CBLQueryExpression all]]]; *listQuery = [CBLQueryBuilder select:@[count] - from:[CBLQueryDataSource database:dbName]] // <.> + from:[CBLQueryDataSource database:db]] // <.> // end::query-syntax-count-only[] @@ -1686,12 +1683,14 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl // tag::query-syntax-id[] +CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error]; + CBLQuery *listQuery; CBLQuerySelectResult *id = [CBLQuerySelectResult expression:[CBLQueryMeta id]]; *listQuery = [CBLQueryBuilder select:@[id] - from:[CBLQueryDataSource database:dbName]] + from:[CBLQueryDataSource database:db]] // end::query-syntax-id[] @@ -1722,11 +1721,12 @@ - (void)close:(nullable NSError *)error completion:(nonnull void (^)(void))compl // tag::query-syntax-pagination[] int thisOffset = 0; int thisLimit = 20; +CBLDatabase *db = [[CBLDatabase alloc] initWithName:@"hotels" error: &error]; CBLQuery* listQuery = [CBLQueryBuilder select: @[[CBLQuerySelectResult all]] - from: [CBLQueryDataSource database: this_Db] + from: [CBLQueryDataSource database: db] limit: [CBLQueryLimit limit: [CBLQueryExpression integer: thisLimit] offset: [CBLQueryExpression integer: thisOffset]]