0

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. The backend has a simple API that returns new data in JSON depending on the applications latest synchronization date. 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. 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 loadForCategory:row]; rows the loop runs just fine. The ImageHandler class just checks if the image exists and downloads it if it doesn't.

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):

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.

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. This has happened before when updating / inserting with fmdb.

Edit2: I have narrowed it down to this UPDATE query. It locks the whole application without any errors. [db executeUpdate:@"UPDATE categories SET number_places = 21 WHERE id = 44"];

Stefan Edberg
  • 231
  • 2
  • 4
  • 15

2 Answers2

1

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. You'll have to look there for the cause.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Yeah, youre right. I've commented out bit for bit in my loadForItem and it seems to be a problem when calling the delegates. I'm posting the method in my question above. – Stefan Edberg Apr 18 '12 at 11:32
  • 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. This has happened before when updating / inserting with fmdb. – Stefan Edberg Apr 18 '12 at 11:54
0

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);
meronix
  • 6,175
  • 1
  • 23
  • 36