当我成功地发布到服务器时,我使用AFNetworking和FMDB从数组中删除数据库行。我目前的问题是,我的日志记录显示我跳过了数组中的每一个项,所以我认为我在AFNetworking的异步请求中错误地使用了FMDB。
这就是代码的样子:
[manager POST:[_urlEndpoint absoluteString] parameters:data success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
[_dbQueue inDatabase:^(FMDatabase *db) {
for (int i = 0; i < arrayOfIds.count; ++i)
{
NSLog(@"Removing event at index: %@", arrayOfIds[i]);
[_db removeWithId:arrayOfIds[i]];
}
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];在arrayOfIds中,日志显示我命中了1、2、3、skip 4等等。在这个特定的例子中,我确保只发送一个POST请求,这样就缩小了根本原因,所以我知道arrayOfIds没有多个访问。
我可能做错了什么,而我可能错过了?
(可能)我刚才问过的相关问题:FMDB: Does calling a method to make a query in the inDatabase block work the same way?
编辑:这里是我的日志看起来像..。
2014-07-13 03:08:28.684 PostTestApp[77268:6675567] Adding event to array to be sent: 1
2014-07-13 03:08:28.684 PostTestApp[77268:6675567] Adding event to array to be sent: 2
2014-07-13 03:08:28.684 PostTestApp[77268:6675567] Adding event to array to be sent: 3
2014-07-13 03:08:28.684 PostTestApp[77268:6675567] Adding event to array to be sent: 4
2014-07-13 03:08:28.684 PostTestApp[77268:6675567] Adding event to array to be sent: 5
2014-07-13 03:08:28.684 PostTestApp[77268:6675567] Adding event to array to be sent: 6
2014-07-13 03:08:29.191 PostTestApp[77268:6675567] JSON: {
description = "Server response from /events";
name = response;
}
2014-07-13 03:08:29.192 PostTestApp[77268:6675567] Removing event at index: 1
2014-07-13 03:08:29.192 PostTestApp[77268:6675567] Removed event from row: 1
2014-07-13 03:08:29.195 PostTestApp[77268:6675567] Removing event at index: 3
2014-07-13 03:08:29.195 PostTestApp[77268:6675567] Removed event from row: 3
2014-07-13 03:08:29.196 PostTestApp[77268:6675567] Removing event at index: 5
2014-07-13 03:08:29.197 PostTestApp[77268:6675567] Removed event from row: 5"Adding event to array to be sent:"是一个for循环,它将数据库is添加到arrayOfIds中。"Removing event at index:"发生在inDatabase:块中。"Removed event from row:"发生在removeWithId:内部。编辑2:
没有编辑的完整代码块:https://gist.github.com/jonalmeida/27bee72b9015d45434e8
发布于 2014-07-13 07:31:13
根据您的要点,您的实际代码如下所示:
for (int i=0; i < dbIndexArray.count; i++) {
NSLog(@"Removing event at index: %@", dbIndexArray[i]);
[_db removeEventWithId:[[dbIndexArray objectAtIndex:i] longLongValue]];
[dbIndexArray removeObjectAtIndex:i];
}问题是:在迭代数组时,您正在修改它。当您删除对象0,而对象1变成对象0时。
试试这个:
NSMutableArray *removedIDs = [NSMutableArray array];
for (int i=0; i < dbIndexArray.count; i++) {
NSLog(@"Removing event at index: %@", dbIndexArray[i]);
[_db removeEventWithId:[[dbIndexArray objectAtIndex:i] longLongValue]];
[removedIDs addObject:dbIndexArray[i];
}
[dbIndexArray removeObjectsInArray:removedIDs];https://stackoverflow.com/questions/24719913
复制相似问题