使用AWSDynamoDBScanExpression查找列表中包含密钥的所有对象

时间:2015-12-31 21:23:20

标签: ios objective-c amazon-dynamodb fetch aws-sdk

我有一个Keys [key1,key2,key3]列表(在我的groups对象中作为NSArray),我想使用Objective-C找到DynamoDB Groups表中的所有值。

应该是一个简单的任务,但我遇到了一个错误,我对如何为filterExpression对象形成AWSDynamoDBScanExpression感到困惑:

AWSDynamoDBScanExpression *sc = [AWSDynamoDBScanExpression new];

sc.limit = @10;
sc.filterExpression = @"GroupID IN :val";
sc.expressionAttributeValues = @{@":val":groups};

[[dynamoDBObjectMapper scan:[Group class] expression:sc] continueWithBlock:^id(AWSTask *task) {
              if (task.error) {
                  NSLog(@"The request failed. Error: [%@]", task.error);
              }
              if (task.exception) {
                  NSLog(@"The request failed. Exception: [%@]", task.exception);
              }
              if (task.result) {
                  AWSDynamoDBPaginatedOutput *output = task.result;
                  NSArray *items = output.items; 
             }
              return nil;
          }];

错误:

  

请求失败。错误:[错误域= com.amazonaws.AWSDynamoDBErrorDomain代码= 0“(null)”UserInfo = {__ type = com.amazon.coral.validate#ValidationException,message = Invalid FilterExpression:语法错误; token:“:val”,附近:“IN:val”}]

1 个答案:

答案 0 :(得分:2)

  1. 如果你想找到你应该做的所有值

    [[dynamoDBObjectMapper scan:[Group class] [AWSDynamoDBScanExpression new]] continueWithBlock:^id(AWSTask *task) {
        //do stuff with (AWSDynamoDBPaginatedOutput obj).items -> should contain object of class Group
        return nil;
    }];
    
  2. 如果您想添加一些过滤器,可以配置AWSDynamoDBScanExpression类似

    的内容
    AWSDynamoDBScanExpression *scanExpression = [[AWSDynamoDBScanExpression alloc] init];
    
    scanExpression.expressionAttributeNames = @{
                                                @"#P": [NSString stringWithFormat:@"%@", <propertyNameToFileter>]
                                                };
    scanExpression.filterExpression = @"#P = :val";
    scanExpression.expressionAttributeValues = @{
                                                 @":val" : <filterCriteriaForProperty>
                                                 };
    
  3. 并调用相同的扫描方法。

    在这两种情况下,你都会收到对象AWSDynamoDBPaginatedOutput

      

    打印任务说明 - &gt; _result:   AWSDynamoDBPaginatedOutput:0x7ff8faf56cc0

    enter image description here