首先,我想用DynamoDB实现的目标:
- > (我试图在登录系统的扫描过滤器中包含用户名和密码。因此,如果对于电子邮件/用户和密码组合,scan
AWSDynamoDBObjectMapper
方法返回一个对象,则登录成功,否则不。)
出了什么问题?
我一直在努力阅读Github上的文档和示例代码,但似乎都没有正确使用它的示例。从文档中我只发现scanFilter
是NSDictionary类型。所以我尝试包含多个参数用于过滤(UserName/UserEmail and Password
),并且无法扫描结果。
scanExpression.scanFilter = @{@"Password": passwordCondition, UserNameOrUserEmail : usernameOrEmailCondition};
但是,如果我只传递一个参数,它会工作并返回一个对象,或者说一行数据库。
scanExpression.scanFilter = @{@"Password": passwordCondition};
完整的参考代码(类似的Android代码可以正常工作,当我提到同事的时候,谁在做Android。)
// Check if email or username and decide DynamoDB Attribute accordingly.
NSString *UserNameOrUserEmail;
if ([usernameOrEmail rangeOfString:@"@"].location == NSNotFound)
UserNameOrUserEmail = @"UserName";
else
UserNameOrUserEmail = @"UserEmail";
AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new];
AWSDynamoDBCondition *passwordCondition = [AWSDynamoDBCondition new];
AWSDynamoDBAttributeValue *passwordAttribute = [AWSDynamoDBAttributeValue new];
passwordAttribute.S = password;
passwordCondition.attributeValueList = @[passwordAttribute];
passwordCondition.comparisonOperator = AWSDynamoDBComparisonOperatorEQ;
AWSDynamoDBCondition *usernameOrEmailCondition = [AWSDynamoDBCondition new];
AWSDynamoDBAttributeValue *usernameOrEmailAttribute = [AWSDynamoDBAttributeValue new];
usernameOrEmailAttribute.S = @"sammy@hhd.com";//usernameOrEmail;
usernameOrEmailCondition.attributeValueList = @[usernameOrEmailAttribute];
usernameOrEmailCondition.comparisonOperator = AWSDynamoDBComparisonOperatorEQ;
//Apply filter condtions: http://stackoverflow.com/a/27856299/818506
scanExpression.scanFilter = @{@"Password": passwordCondition, UserNameOrUserEmail : usernameOrEmailCondition}; //@{@"UserEmail": usernameOrEmailCondition};//
//^^^^^^^^^^^^^^^^^^^^^^^^^^^Is this correct?^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//Perform a scan:
[[dynamoDBObjectMapper scan:[User class] expression:scanExpression]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 *paginatedOutput = task.result;
for (User *user in paginatedOutput.items) {
isLoginSuccessful = YES;
}
completionHandler(isLoginSuccessful);
}
return nil;
}];
任何建议/想法/评论都会受到赞赏。
谢谢, 罗汉。
答案 0 :(得分:0)
我在处理类似问题的时候发现了这个帖子。以下是我解决它的方法:
let objectMapper = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.filterExpression = "#UserNameOrUserEmail = :UserNameOrUserEmail AND #password = :follows"
scanExpression.expressionAttributeNames = ["#UserNameOrUserEmail": "UserNameOrUserEmail" , "#password": "password" ,]
scanExpression.expressionAttributeValues = [":UserNameOrUserEmail": "sammy@hhd.com", ":password": password ,]
objectMapper.scan(User.self, expression: scanExpression) { (response: AWSDynamoDBPaginatedOutput?, error: Error?) in
// handle data or error here...
}
只是它可以帮助别人。