gpt4 book ai didi

android - AWS DynamoDB 扫描在 iOS 上失败但在 Android 上运行

转载 作者:行者123 更新时间:2023-11-29 01:03:43 24 4
gpt4 key购买 nike

我使用 DynamoDB 作为我的移动应用程序的后端数据库,并且架构等在 Android 和 iOS 上是相同的。对于特定用例,我必须根据两个未索引的属性执行扫描。对于 iOS Objective C,我使用以下代码:

AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new];
scanExpression.limit = [NSNumber numberWithInt:maxCount];
scanExpression.filterExpression = @"#l = :location AND event = :event";
scanExpression.expressionAttributeNames = @{@"#l":@"location"};
scanExpression.expressionAttributeValues = @{@":location":location,
@":event":EVENT_TASTING};

位置和事件都是字符串。 EVENT_TASTING 是一个字符串常量。尽管我已经验证对于所提供的条目我应该收到结果,但此扫描始终返回零结果。我在 Android Java 中使用以下代码:

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
scanExpression.setLimit(maxCount);

scanExpression.addFilterCondition("location",
new Condition()
.withComparisonOperator(ComparisonOperator.EQ)
.withAttributeValueList(new AttributeValue().withS(location)));
scanExpression.addFilterCondition("event",
new Condition()
.withComparisonOperator(ComparisonOperator.EQ)
.withAttributeValueList(new AttributeValue().withS(Constants.EVENT_TASTING)));

扫描在 Android 中按预期工作。 iOS 中需要进行哪些更改才能使其在 iOS 上也能正常工作?我将 iOS SDK 更新到了 2.3.6,但没有产生任何影响。这是我在代码中执行的唯一扫描操作。

我的 iOS 版 scanExpression 是否有错误?有没有办法使用 Android 风格的语法来使其在 iOS 上工作?

更新

我尝试了以下更改:

   AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new];
AWSDynamoDBAttributeValue *locationVal = [AWSDynamoDBAttributeValue new];
locationVal.S = location;
AWSDynamoDBAttributeValue *eventVal = [AWSDynamoDBAttributeValue new];
eventVal.S = EVENT_TASTING;
scanExpression.limit = [NSNumber numberWithInt:maxCount];
scanExpression.filterExpression = @"#l = :location AND event = :event";
scanExpression.expressionAttributeNames = @{@"#l":@"location"};
scanExpression.expressionAttributeValues = @{@":location":locationVal,
@":event":eventVal};

但是现在我得到一个错误:

 The request failed. Error: [Error Domain=com.amazonaws.AWSDynamoDBErrorDomain Code=0 "(null)" UserInfo={message=ExpressionAttributeValues contains invalid value: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes for key :location, __type=com.amazon.coral.validate#ValidationException}]

最佳答案

感谢@YosukeMatsuda 的提示,我能够通过重复调用 Scan 直到 LastEvaluatedKey 为空来解决此问题。我将此作为答案发布,因为不幸的是 Mike 的答案没有指出正确的问题并且具有误导性。

以下是我在 iOS 中更改代码的方式:

// In a different method (for first call):
AWSDynamoDBScanExpression *scanExpression = // See code in original question

// In a new method that can be called recursively:
// DynamoDBObjectMapper scan:class-for-model expression:scanExpression
// continueWithBlock -> if (task.result):
AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
if (paginatedOutput.items.count != 0)
// Append the paginatedOutput.items to the cumulative array
else
// Replace the cumulative array with paginatedOutput.items
if (paginatedOutput.lastEvaluatedKey.count == 0) {
// Scan is complete - handle results
} else {
// Check if you have sufficient results
// In my case I had asked for 25 results but was getting 39
// So it doesn't seem to obey the scanExpression.limit value
// If more results are needed, continue the scan
[scanExpression setExclusiveStartKey:paginatedOutput.lastEvaluatedKey];
// Call this method recursively
}

如果有更优雅的解决方案,我很想听听。但至少它现在有效。

关于android - AWS DynamoDB 扫描在 iOS 上失败但在 Android 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36683659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com