我有一个表,将有关组(GroupID、Members、Creator、LastAccessed、GroupName 等)的信息存储为单独的行。每个组都有一个唯一标识符 (GroupID) 作为它们的散列(主键)。它们还有一个名为 GroupName 的属性。我有一个搜索框,用户可以在其中输入部分组名。我想对表执行扫描并返回以用户输入开头的所有组。这是我到目前为止所拥有的..
func searchForGroupsWithName(groupName: String) {
self.queryInProgress = true
let cond = AWSDynamoDBCondition()
let v1 = AWSDynamoDBAttributeValue();
v1.S = groupName
cond.comparisonOperator = AWSDynamoDBComparisonOperator.BeginsWith
cond.attributeValueList = [ v1 ]
let exp = AWSDynamoDBScanExpression()
//I only want to return the GroupName and GroupID.
//I think this should be ["GroupID", "GroupName"], but it requires a string
exp.projectionExpression = ??????????
//I am not sure how to incorporate cond with this.
exp.filterExpression = ??????????
dynamoDBObjectMapper.scan(GroupTableRow.self, expression: exp).continueWithBlock({ (task:AWSTask!) -> AnyObject! in
if task.result != nil {
let paginatedOutput = task.result as! AWSDynamoDBPaginatedOutput
for item in paginatedOutput.items as! [GroupTableRow] {
self.searchBarResults.append(item)
}
if ((task.error) != nil) {
print("Error: \(task.error)")
}
self.queryInProgress = false
return nil
}
self.queryInProgress = false
return nil
})
}
最佳答案
在获得 WestonE 的帮助后,我修正了我的代码并有了一个工作示例。我已将其粘贴在下面,供需要类似内容的其他人使用
func searchForGroupsWithName(groupName: String) {
self.queryInProgress = true
let lowercaseGroupName = groupName.lowercaseString
let scanExpression = AWSDynamoDBScanExpression()
//Specify we want to only return groups whose name contains our search
scanExpression.filterExpression = "contains(LowercaseName, :LowercaseGroupName)"
//Create a scan expression to state what attributes we want to return
scanExpression.projectionExpression = "GroupID, GroupName, LowercaseName"
//Define our variable in the filter expression as our lowercased user input
scanExpression.expressionAttributeValues = [":LowercaseGroupName" : lowercaseGroupName]
dynamoDBObjectMapper.scan(GroupTableRow.self, expression: scanExpression).continueWithBlock({ (task:AWSTask!) -> AnyObject! in
if task.result != nil {
let paginatedOutput = task.result as! AWSDynamoDBPaginatedOutput
//use the results
for item in paginatedOutput.items as! [GroupTableRow] {
}
if ((task.error) != nil) {
print("Error: \(task.error)")
}
self.queryInProgress = false
return nil
}
self.queryInProgress = false
return nil
})
}
关于ios - 如何在 swift 中使用 filterExpression 执行 DynamoDB 扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38859518/