[英]Calling delegate in loop works only the first time
Some background info 一些背景信息
I'm making an app which has a adminsitration backend for updating it's data. 我正在制作一个具有管理后端的应用程序,用于更新其数据。 The app saves all data in an SQLite datbase for offline support.
该应用程序将所有数据保存在SQLite数据库中,以提供离线支持。 The backend has a simple API that returns new data in JSON depending on the applications latest synchronization date.
后端有一个简单的API,可以根据应用程序的最新同步日期以JSON返回新数据。 The data contains images which is downloaded and saved in the app.
数据包含已下载并保存在应用程序中的图像。
The problem 问题
I have solved most of it but got stuck at this problem. 我已经解决了大部分问题,但被这个问题困扰。 When the data is loaded I loop through it and initiate the ImageHandler class which will download the image, save it and insert a row in the database.
加载数据后,我遍历数据并启动ImageHandler类,该类将下载图像,保存图像并在数据库中插入一行。 This works fine the first time but it doesn't go past the first data row in the loop.
第一次可以很好地运行,但是不会超出循环中的第一个数据行。 If i uncomment the [imageHandler loadForPlace:row];
如果我取消注释[imageHandler loadForPlace:row]; / [imageHandler loadForCategory:row];
/ [imageHandler loadForCategory:row]; rows the loop runs just fine.
行循环运行得很好。 The ImageHandler class just checks if the image exists and downloads it if it doesn't.
ImageHandler类仅检查图像是否存在,如果不存在则下载图像。
for (NSDictionary *row in data) {
NSLog(@"Action: %@. Loading images...", [row objectForKey:@"action"]);
imageHandler = [[Image alloc] init];
imageHandler.delegate = self;
if([[row objectForKey:@"action"] isEqualToString:@"CREATE_POST"] || [[row objectForKey:@"action"] isEqualToString:@"UPDATE_POST"]) {
NSLog(@"1 (pre)");
[imageHandler loadForPlace:row];
NSLog(@"1");
}
else {
NSLog(@"2 (pre)");
[imageHandler loadForCategory:row];
NSLog(@"2");
}
}
Output: 输出:
2012-04-18 10:32:08.698 Appname[17636:11603] Syncronize: Request done
2012-04-18 10:32:08.698 Appname[17636:11603] Found 2 items to create or update.
2012-04-18 10:32:08.698 Appname[17636:11603] Action: CREATE_POST. Loading images...
2012-04-18 10:32:08.699 Appname[17636:11603] 1 (pre)
2012-04-18 10:32:08.699 Appname[17636:11603] id: 778
2012-04-18 10:32:08.700 Appname[17636:11603] Image already exists, continuing...
2012-04-18 10:32:08.700 Appname[17636:11603] Image(s) saved
2012-04-18 10:32:08.700 Appname[17636:11603] Create place
2012-04-18 10:32:08.701 Appname[17636:11603] Has image
loadForItem (called by loadForPlace and loadForCategory): loadForItem(由loadForPlace和loadForCategory调用):
As you can see I commented out the delegate calls since they are the ones causing the loop not to continue. 如您所见,我注释掉了委托调用,因为它们是导致循环无法继续进行的调用。 When I run this the loop continues without a problem.
当我运行此循环将继续没有问题。 With my test data I currently end up in the "Image already exists, continuing..." part of loadForItem so it doesn't do anything when the delegate code is commented out.
使用我的测试数据,我当前最终位于loadForItem的“图像已存在,正在继续...”部分,因此当注释掉委托代码时,它不会执行任何操作。
Edit: Okay, I don't know why I haven't noticed this before, but the application hangs withouth any errors outputed to the console. 编辑:好的,我不知道为什么以前没有注意到这一点,但是应用程序挂起,没有任何错误输出到控制台。 Damn!
该死的! It's when the delegate calls my method after checking the images that crashes this.
这是委托检查图像后使我的方法崩溃的原因。 That method inserts / updates the place in my sqlite database and I'm using fmdb which crashes my app without any errors.
该方法将插入/更新我的sqlite数据库中的位置,并且我正在使用fmdb,这会使我的应用程序崩溃而没有任何错误。 This has happened before when updating / inserting with fmdb.
这是在使用fmdb更新/插入之前发生的。
Edit2: I have narrowed it down to this UPDATE query. Edit2:我已将其范围缩小到此UPDATE查询。 It locks the whole application without any errors.
它锁定整个应用程序,没有任何错误。
[db executeUpdate:@"UPDATE categories SET number_places = 21 WHERE id = 44"];
Since NSLog(@"1 (pre)")
is evidently called, from the log output you provided, but NSLog(@"1")
is not, that suggests that -[Image loadForPlace:]
is not returning. 由于显然是从您提供的日志输出中调用了
NSLog(@"1 (pre)")
没有调用NSLog(@"1")
,这表明-[Image loadForPlace:]
没有返回。 You'll have to look there for the cause. 您必须在那寻找原因。
are you sure that data have more than 1 item? 您确定数据有多个项目吗? when you call:
你打电话时:
for (NSDictionary *row in data) {
// (...)
}
try to log a count: 尝试记录计数:
NSLog(@"data.count: %i", data.count);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.