我正在使用带有 Xcode 7.2.1 的 DBAccess 框架 v1.6.12。
我想通过指定 ID 来删除多行。
代码图片:
// MyDataModel is a subclass of DBObject
const int startCount = [[MyDataModel query] count];
NSLog(@"startCount:%@", @(startCount)); // startCount:21
NSString *Ids = [removeIds componentsJoinedByString:@","];
NSLog(@"removingIds:%@", Ids); // removingIds:48,44,49,45,50,46,51,47,43
DBResultSet *rs = [[[MyDataModel query] whereWithFormat:@"Id in (%@)" withParameters:@[Ids]] fetch];
NSLog(@"resultSetCount:%@", @(rs.count)); // resultSetCount:0
[rs removeAll];
const int afterCount = [[MyDataModel query] count];
NSLog(@"afterCount:%@", @(afterCount)); // afterCount:21
也许,DBAccess 不支持"Id in (?,?,?)"
格式查询?
请告诉我上面存档的解决方案。
[附加]
当我编写如下代码时,它按预期工作。
难道这,会是正当的措施吗?
for (NSNumber *Id in removeIds) {
[[[[MyDataModel query] whereWithFormat:@"Id = %@" withParameters:@[Id]] fetchLightweight] removeAll];
}
[回复#1]
感谢您的评论。
我试过你的建议。但它无法解决。
NSLog(@"before-delete:%@", @([[MyDataModel query] count]));
// -> before-delete:23
if (removeIds.count) {
NSLog(@"removeIds:%@", [removeIds componentsJoinedByString:@","]);
// -> removeIds:119,120
[[[[MyDataModel query] whereWithFormat:@"Id in (%@)" withParameters:removeIds] fetchLightweight] removeAll];
}
NSLog(@"after-delete:%@", @([[MyDataModel query] count]));
// -> after-delete:22
[removeIds enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"Id[%@]=%@", obj, @([[[MyDataModel query] whereWithFormat:@"Id = %@" withParameters:@[obj]] count]));
// -> Id[119]=0 .. 0 means succeeded to delete
// -> Id[120]=1 .. 1 means failed to delete
}];
我想删除 Id = 119 和 Id = 120,但它只删除了一行。
有效!
毕竟,我写了下面的代码。
if (removeIds.count) {
[[[[MyDataModel query] whereWithFormat:@"Id IN (%@)" withParameters:@[removeIds]] fetchLightweight] removeAll];
}
感谢您的帮助!