Home > Back-end >  DynamoDB Query using Objective-C throwing unrecognized selector exception
DynamoDB Query using Objective-C throwing unrecognized selector exception

Time:04-09

I am trying to republish an old iPhone app written in Objective-C. Decided to use DynamoDB but have been struggling to query using AWSDynamoDBQueryInput.

Problem: need to retrieve top N records by descending order on a sort key from a global secondary index. Global secondary index defined with a hash key (gameTitle) and sort key (totalScore). I have been following AWS documentation and can get it to work in the CLI but have not been able to do the same through Xcode/Objective-C. I am getting an "unrecognized selector sent to class" exception.

AWSDynamoDBQueryInput *AWSquery = [AWSDynamoDBQueryInput new];

AWSquery.tableName = AWSTableName;
AWSquery.indexName = AWSIndex;
AWSquery.scanIndexForward = false;
AWSquery.limit = @6;
AWSquery.keyConditionExpression = @"gameTitle = :gameTitle";
AWSquery.expressionAttributeValues = @{@":gameTitle":@"sampleGame"};

@try {
      [[dynamoDB query:AWSquery] continueWithBlock:^id(AWSTask *task){
            return @"task.result";
        }];
    }
    
@catch (NSException *exception) {
      NSLog(@"%@ ", exception.name);
      NSLog(@"Reason: %@ ", exception.reason);
    }

I suspect the issue is with "keyConditionExpression" but any guidance is much appreciated. Thank you!

CodePudding user response:

Finally got it to work! I had to use AWSDynamoDBAttributeValue to define the attribute value before using it in expressionAttributeValues. Working code below.

AWSDynamoDBQueryInput *AWSquery = [AWSDynamoDBQueryInput new];

AWSquery.tableName = AWSTableName;
AWSquery.indexName = AWSIndex;
AWSquery.scanIndexForward = @NO;
AWSquery.limit = @6;

AWSDynamoDBAttributeValue *attValue = [AWSDynamoDBAttributeValue new];
attValue.S = @"sampleGame";
AWSquery.keyConditionExpression = @"gameTitle=:gameTitle";
AWSquery.expressionAttributeValues = @{@":gameTitle":attValue};

@try {
      [[dynamoDB query:AWSquery] continueWithBlock:^id(AWSTask *task){
            return @"task.result";
        }];
    }
    
@catch (NSException *exception) {
      NSLog(@"%@ ", exception.name);
      NSLog(@"Reason: %@ ", exception.reason);
    }
  • Related