Lowip
Lowip

Reputation: 441

KVO Unexpected Crash

I have a UITableView whose height changes at runtime : the app loads images and then displays them in the TableView. I've implemented a pull to refresh like feature for the footer of my tableview.

Since height of the rows is not constant, I have added an observer on my UITableView:

[self.tableView addObserver:_refreshFooter
                 forKeyPath:@"contentSize"
                    options:NSKeyValueObservingOptionNew
                    context:NULL];

In my _refreshFooter, the following method is called as soon as the contentSize changes :

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
  UITableView *tbV = object;

  float realBottom = MAX(tbV.frame.size.height, tbV.contentSize.height);
  self.frame = CGRectMake(self.frame.origin.x,
                          realBottom,
                          self.frame.size.width,
                          self.frame.size.height);
}

The app is basically a tabbar app where each tab is an instance of the TableViewController. On our iPhone 4 and 4S in 5.0.1 and 5.1, we have never experienced any crashes due to KVO but our users tells us unexpected behaviour such as the footer not being moved or crash.

The crash experienced is the following :

0   libobjc.A.dylib 0x3671bf78 objc_msgSend + 15
1   Foundation 0x31145659 NSKeyValuePushPendingNotificationPerThread + 68
2   Foundation 0x3113774b NSKeyValueWillChange + 414
3   Foundation 0x3110e84f -[NSObject(NSKeyValueObserverNotification) willChangeValueForKey:] + 182
4   Foundation 0x31190b23 _NSSetSizeValueAndNotify + 82
5   UIKit 0x32368f8d -[UITableView(_UITableViewPrivate) _updateContentSize] + 572
6   UIKit 0x3249cd51 -[UITableView setTableFooterView:] + 432

Upvotes: 3

Views: 1925

Answers (1)

rishi
rishi

Reputation: 11839

Release NSKeyValueObservingOptionNew in viewWillDisapper and add this is viewWillAppear.

Upvotes: 2

Related Questions