Skip to content

Instantly share code, notes, and snippets.

@abbood
Last active December 19, 2015 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abbood/5874394 to your computer and use it in GitHub Desktop.
Save abbood/5874394 to your computer and use it in GitHub Desktop.
-(void)loadResults:(NSArray*)searchResults {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSMutableArray* elementsToAdd = [NSMutableArray arrayWithCapacity:100];
NSMutableArray* rowsToAdd = [NSMutableArray arrayWithCapacity:100];
@synchronized(self) {
for(NSMutableDictionary* searchResult in searchResults) {
// adding an entry from syncing new items
[elementsToAdd addObject:searchResult];
[rowsToAdd addObject:[NSIndexPath indexPathForRow:self.nResults inSection:0]];
self.nResults++;
}
[searchResults release]; // it was retained in SearchRunner!
if([elementsToAdd count] > 0) {
NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:elementsToAdd, @"data", rowsToAdd, @"rows", nil]; // released in insertRows()
[self performSelectorOnMainThread:@selector(insertRows:) withObject:info waitUntilDone:NO];
}
}
[pool release];
}
-(void)insertRows:(NSDictionary*)info {
@try {
NSArray* y = [info objectForKey:@"data"];
BOOL insertNew = (([y count] == 1) && [[[y objectAtIndex:0] objectForKey:@"syncingNew"] boolValue]);
NSLog(@"this is attachmentData count before insert %d", [self.attachmentData count]);
if(insertNew) {
NSLog(@":: inserting new ---> inserting from y at index 0");
[self.attachmentData insertObject:[y objectAtIndex:0] atIndex:0];
} else {
NSLog(@":: NOT inserting new -----> addobjectsfromArray y");
[self.attachmentData addObjectsFromArray:y];
}
NSLog(@"this is attachmentData count after insert %d AND BEFORE we call insert rows", [self.attachmentData count]);
[self.tableView insertRowsAtIndexPaths:[info objectForKey:@"rows"] withRowAnimation:UITableViewRowAnimationNone];
} @catch (NSException *exp) {
NSLog(@"Exception in insertRows: %@", exp);
NSLog(@":::::: info[rows]: %@ \n::::::attachmentData.count: %i\n :::::: info retaincount: %i\n:::::: info[data] retain count:%i\n :::::: info[rows] retain count: %i", [info objectForKey:@"rows"], [self.attachmentData count], [info retainCount], [[info objectForKey:@"data"] retainCount], [[info objectForKey:@"rows"] retainCount]);
}
[info release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int add = 1;
NSLog(@"::: number of rows in section.. attachmentData count is %d", [self.attachmentData count]);
return [self.attachmentData count] + add;
}
@abbood
Copy link
Author

abbood commented Jun 27, 2013

the error i'm getting (some of the logs above are not included here b/c this i got this error before i put'em):

2013-06-27 09:11:49.487 Breeze[24235:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070
2013-06-27 09:11:49.487 Breeze[24235:c07] Exception in insertRows: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
2013-06-27 09:11:49.488 Breeze[24235:c07] :::::: info[rows]: (
"<NSIndexPath 0xb656940> 2 indexes [0, 0]"
)
::::::attachmentData.count: 1
:::::: info retaincount: 2
:::::: info[data] retain count:1
:::::: info[rows] retain count: 1

@abbood
Copy link
Author

abbood commented Jun 27, 2013

so the reason why we have number of rows = 2 is b/c we got this code:

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    int add = 1;

    return [self.attachmentData count] + add;
    }
    so we always have one extra row (for is loading etc) than what's in self.attachmentData.. however, self.attachmentData is supposed to have 2 elements stored in it (at least according to the error msg above).. but it DOESN'T..
    so the conclusion is that self.attachmentData is not getting updated fast enough?

@abbood
Copy link
Author

abbood commented Jun 27, 2013

updated error log (including all the ones from above):

2013-06-27 09:41:47.063 Breeze[25225:c07] this is attachmentData count before insert 0
2013-06-27 09:41:47.063 Breeze[25225:c07] :: NOT inserting new -----> addobjectsfromArray y
2013-06-27 09:41:47.064 Breeze[25225:c07] this is attachmentData count after insert 1 AND BEFORE we call insert rows
2013-06-27 09:41:47.064 Breeze[25225:c07] ::: number of rows in section.. attachmentData count is 1
2013-06-27 09:41:47.065 Breeze[25225:c07] ::: number of rows in section.. attachmentData count is 1
2013-06-27 09:41:47.065 Breeze[25225:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070
2013-06-27 09:41:47.066 Breeze[25225:c07] Exception in insertRows: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
2013-06-27 09:41:47.066 Breeze[25225:c07] :::::: info[rows]: (
"<NSIndexPath 0xb45ea80> 2 indexes [0, 0]"
)
::::::attachmentData.count: 1
:::::: info retaincount: 2
:::::: info[data] retain count:1
:::::: info[rows] retain count: 1

@abbood
Copy link
Author

abbood commented Jun 27, 2013

updated error log (including all the ones from above):

2013-06-27 09:41:47.063 Breeze[25225:c07] this is attachmentData count before insert 0
2013-06-27 09:41:47.063 Breeze[25225:c07] :: NOT inserting new -----> addobjectsfromArray y
2013-06-27 09:41:47.064 Breeze[25225:c07] this is attachmentData count after insert 1 AND BEFORE we call insert rows
2013-06-27 09:41:47.064 Breeze[25225:c07] ::: number of rows in section.. attachmentData count is 1
2013-06-27 09:41:47.065 Breeze[25225:c07] ::: number of rows in section.. attachmentData count is 1
2013-06-27 09:41:47.065 Breeze[25225:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070
2013-06-27 09:41:47.066 Breeze[25225:c07] Exception in insertRows: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
2013-06-27 09:41:47.066 Breeze[25225:c07] :::::: info[rows]: (
"<NSIndexPath 0xb45ea80> 2 indexes [0, 0]"
)
::::::attachmentData.count: 1
:::::: info retaincount: 2
:::::: info[data] retain count:1
:::::: info[rows] retain count: 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment