【发布时间】:2025-12-10 09:55:02
【问题描述】:
我正在努力增加已保存在我的 DynamoDB 表中的项目的数字属性值,我的代码当前是:
AWSDynamoDBUpdateItemInput *updateItemInput = [AWSDynamoDBUpdateItemInput new];
updateItemInput.tableName = @"Table";
updateItemInput.key= @{
@"KeyPropertyName":@"KeyValue"
};
updateItemInput.updateExpression = @"SET(counter = counter + :val)";
updateItemInput.expressionAttributeValues =@{
@":val":@1
};
AWSDynamoDB *dynamoDB = [AWSDynamoDB defaultDynamoDB];
[[dynamoDB updateItem:updateItemInput]
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) {
//Do something with result.
}
return nil;
}];
当我不注释掉 updateExpression 和 expressionAttributeValues 时,我的应用程序总是崩溃。当我创建我的项目类型的空实例时,我可以到达块,当我创建 AWSDynamoDBAttributeValue 的实例以传递密钥时,我会得到奇怪的结果。有什么建议么?我的 updateExpression 是否也写得不正确?
我还将在另一个对象的数组/列表和字典/映射属性中添加更新和删除项目。这会有什么不同?
【问题讨论】:
-
它是如何崩溃的?您收到的确切错误消息是什么?
-
是的,更新表达式应该没有括号,您可能还需要参数化属性名称
-
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+[__NSCFNumber JSONKeyPathsByPropertyKey]:无法识别的选择器发送到类 0x113d10368”
-
updateItemInput.key= @{ @"KeyPropertyName":[AWSItem new] };这将返回完成块 task.result 但不更新对象
-
@MikeDinescu 只要不注释掉表达式属性值,它就会一直崩溃并出现该错误
标签: ios amazon-web-services amazon-dynamodb updates increment