首先,我试图用DynamoDB实现的目标:
->(我试图在登录系统的扫描过滤器中包括用户名和密码。因此,如果对于电子邮件/用户和密码组合,AWSDynamoDBObjectMapper
的scan
方法返回一个对象,则登录成功,否则为否。(
怎么了?
我一直在尝试阅读Github上的文档和示例代码,但似乎没有一个正确使用它的例子。从文档中我只发现scanFilter
属于NSDictionary类型。所以我尝试包含多个用于过滤的参数(UserName/UserEmail and Password
(,但它无法扫描结果。
scanExpression.scanFilter = @{@"Password": passwordCondition, UserNameOrUserEmail : usernameOrEmailCondition};
然而,如果我只传递一个参数,它就会工作并返回一个对象,或者说一行数据库。
scanExpression.scanFilter = @{@"Password": passwordCondition};
完整的代码供参考(当我提到同事的代码时,类似的代码适用于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;
}];
任何建议/想法/意见都将不胜感激。
谢谢,罗翰。
您是否考虑过使用AWSDynamoDBScanInput
和NSMutableDictionary
?我在Swift中有以下例子,应该很容易翻译成Objective-C:
// create password condition
let passwordCondition = AWSDynamoDBCondition()
let v1 = AWSDynamoDBAttributeValue(); v1.S = password
passwordCondition.comparisonOperator = AWSDynamoDBComparisonOperator.EQ
passwordCondition.attributeValueList = [ v1 ]
// create username condition
let usernameCondition = AWSDynamoDBCondition()
let v2 = AWSDynamoDBAttributeValue(); v2.S = "sammy@hhd.com"
usernameCondition.comparisonOperator = AWSDynamoDBComparisonOperator.EQ
usernameCondition.attributeValueList = [ v2 ]
// add these conditions to a NSMutableDictionary
var scanFilterDict : NSMutableDictionary! = NSMutableDictionary()
scanFilterDict.addEntriesFromDictionary(["password": passwordCondition])
scanFilterDict.addEntriesFromDictionary(["UserNameOrUserEmail": usernameCondition])
// use the mutable dictionary as scan filter
scanInput.scanFilter = scanFilterDict as Dictionary<NSObject, AnyObject>
scanInput.exclusiveStartKey = self.lastEvaluatedKey
scanInput.limit = 20
scanInput.tableName = "yourDynamoDBTable"
// apply AND operator on both conditions
scanInput.conditionalOperator = AWSDynamoDBConditionalOperator.And
AWSDynamoDB.defaultDynamoDB().scan(input)
在处理类似问题时,我在这个线程上使用了存根。以下是我解决问题的方法:
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...
}
只是以防万一它能帮助别人。