Search code examples
iosobjective-camazon-web-servicesamazon-dynamodbbolts-framework

Synchronous connecting to AWS DynamoDB in Objective-c


How can I implement synchronous access to AWS DynamoDB in Objective-c?

I understood asynchronous access to DynamoDB using Bolts BFTask as below but I need "synchronous" connecting.

----added some information----

I called "ddbIDQuery" method in "DynamoQuery" class but it returned (null), because of the Bolts asynchronous transaction?? What is the best way to get the result?

// MainViewController.m

#import "DynamoQuery.h"

-(IBAction)ddqButton:(UIButton *)sender
{
    // call DynamoQuery
    DynamoQuery *dynamoQuery = [[DynamoQuery alloc] init];
    NSLog(@"dynamoQuery: %@", [dynamoQuery ddbIDQuery:@"448898329-6BC0FA0A954913043A3281599A444E3C"]);
}


// DynamoQuery.m

- (NSString *) ddbIDQuery: (NSString*)ddbID
{
    __block NSString *strpid = nil;

    AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
    [[dynamoDBObjectMapper load:[PIDTable class] hashKey:ddbID rangeKey:nil] continueWithBlock:^id(AWSTask *task)
     {
         NSLog(@"ddbID: %@", ddbID);
         if (task.error){
             NSLog(@"The 1st request failed. Error: [%@]", task.error);
         }
         if (task.exception) {
             NSLog(@"The 1st request failed. Exception: [%@]", task.exception);
         }
         if (task.result) {
             PIDTable *ddbpid = task.result;
             NSData *datapid = [ddbpid.text dataUsingEncoding:NSUTF8StringEncoding];
             strpid = [[NSString alloc] initWithData:datapid encoding:NSUTF8StringEncoding];
         };
         return nil;
     }
     ];
     return strpid;
}

Solution

  • BFTask has a method called - waitUntilFinished to make it a synchronous method; however, you should avoid it if possible. See Make it synchronous section of our blog for more details.