ios - 如何在 swift 中使用 filterExpression 执行 DynamoDB 扫描

标签 ios swift amazon-web-services amazon-dynamodb

我有一个表,将有关组(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/

相关文章:

iphone - 图片未显示在 iPhone 应用程序的应用商店版本中

iphone - Cocoa touch——获取设备信息

database - Amazon SimpleDB/Amazon RDS 的性能?

amazon-web-services - Apache Camel AWS/SQS 组件

objective-c - 如何在iOS中创建如下图所示的加载屏幕

ios - Google AdMob iPhone 5 iPhone 5S 64 位模拟器问题

ios - 在 swift 5 中设置 UITextField 底部边框的问题

ios - 钥匙串(keychain)服务被代码签名破坏

ios - 当我需要多次从 ARSCNView 切换到其他 View Controller 时,如何管理 ARKit 中的 SCNNode

amazon-web-services - 读取整个表的推荐方法(Lambda、DynamoDB/S3)