NSOutlineView: reloadData and crashes in reloadData

Apple, Cocoa, General

A note to NSOutlineView Cocoa programmers: It has never been safe to call reloadData while an outlineView is doing a reloadData. What the heck does that mean? Well, if you call reloadData, a whole bunch of delegate/datasource methods will be called, such as outlineView:numberOfChildrenOfItem:, outlineView:isItemExpandable:, etc. Well, if in those delegate methods you accidentally call reloadData again, bad things can happen. Specifically, you will probably get a crash in NSOutlineView. More often than not, this is done by an accidental side effect, but it is something to be aware of. On Leopard, I will make NSOutlineView more resilient to this case, and not crash. On Tiger, you can fix it yourself, if you think it is (or may) affect you. Here is the general recipe:

1. Subclass NSOutlineView, and add an iVar that looks like:

   BOOL _isReloading;

2. Override reloadData and make it look something like this:

- (void)reloadData {
    if (!_isReloading) {
       _isReloading = YES;
       [super reloadData];
       _isReloading = NO;
   } else {
       [self performSelector:@selector(reloadData) withObject:nil afterDelay:0];
Previous in objc
WWDC: Beyond Buttons and Sliders: Complex Controls in Cocoa
Next in objc
NSBrowser, bindings, and a custom cell class

Leave a Reply

6 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
4 Comment authors
MichaelSören KuklauMatthew Sachscorbin Recent comment authors
Notify of
Matthew Sachs

The big code sample looks fine, although perhaps the font is a bit small. Did you mean to have “BOOL _isReloading;” and “reloadData” in the “general recipe” inside tt ?

Sören Kuklau

I use code instead of tt (same thing, except with specific semantic meaning), but for what it’s worth, my CSS regarding code and pre looks like this: code, pre { font: 1.25em ‘Courier New’, Courier, Fixed, Monospace; } code, pre { overflow: auto; overflow-x: hidden; } pre { display: block; } The first block increases the font size to ensure (in my case, anyhow) anti-aliasing is on; matter of taste, I guess, but I’ve gotten so used to Quartz’s smooth text rendering that I find non-anti-aliased text on a web page horrible to look at. (Text editors are a different… Read more »


Corbin, I’m a bit new to Cocoa programming and I have a question about NSOutlineViews. I’ve been scouring the web for a simple solution and I feel that I just have to ask this question myself now. What I would like is a static “source list” style outline view. Just to clarify my usage of the term static, the data in the outline view is not user-editable. Would you recommend using a plist to populate this outline view or should it be hardcoded? Do you have any tutorials for doing this? I am also confused about the root object in… Read more »

As an Amazon Associate I earn from qualifying purchases.

(c) 2008-2021 Corbin Dunn

Privacy Policy

Subscribe to RSS feeds for entries.

98 queries. 0.476 seconds.

Log in