NSTableview is randomly empty on startup
I'm hoping this is a common enough problem that a quite a broad question might have a possible obvious answer. Why would my NSTableView
- after the application has loaded - sometimes be filled with data from its connected (Core Data) data source and sometimes be empty. It appears to be random. Nothing else changes between quitting and starting.
To help:
- The
NSTableView
datasource is a singleNSArrayController
that has amanagedObjectContext
bound to an entity core data model. - The
NSTableView
andNSArrayController
are all connected correctly to outlets and bindings. Debugging has worked successfully with any tests I've thought of and the fact that it works randomly suggests something is working. - I am going to assume the problem is an element of my project sometimes being loaded in time and not loaded in time. I had also assumed it was the
NSArrayController
not having fetched its data in time before everything else finished loading. I tried to correct this by asking theNSTableView
toreloadData
after theNSArrayController
finished loading:
[jobsController addObserver:self forKeyPath:@"arrangedObjects.jobTitle" options:0 context:nil];
and
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == jobsController)
{
NSLog(@"jobsController finished loading");
[tableView reloadData];
}
}
I also tested using an isolated test button that triggered reloadData
for the NSTableView
whenever I clicked it. This had no effect. Apologies for the lack of code. The project is rather large and difficult to isolate the code to this problem. I'm hoping this problem is common enough that the answer might be determinable from experience of similar.
Update
I'm pretty confident that it's to do with NSArrayController
's not being ready (as a log of arrangedObjects
reveals an empty set when the table doesn't populate and vice-versa when it does). To expand a little on my set up, I have two other NSArrayController
s (so, 3 in total). The controller that populates the NSTableView
is a child of one controller that's a child of another. I have a theory that the table only populates when the loading order of the NSArrayControllers is (coincidentally) parent > parent > parent as each child relies on its parent NSArrayController
for its content set. Perhaps my solution is in manu开发者_开发问答ally selecting the loading order of the controllers?
In your tableview's awakeFromNib method call the fetch method for the array controller. This will populate the array controller with the required data so that you call reloadData there is actually data to load.
I've found the answer and think this situation might be repeatable for others so hopefully this is useful to them. I had my 'content set' of my NSArrayController
set to its parent NSArrayController
inverse relationship. This made sense earlier in the project when I wanted to populate children arrays with only the selected information of parent arrays.
In this case, this feature was no longer needed and so I'd forgotten it was behaving this way. As a result, when the application started, it was (seemingly) randomly selecting an object in the top NSArrayController
and populating its child NSArrayController
with objects only relevant to its selected parent. If any of these were empty, it would result in no objects filtering down to the child NSArrayController
that populates the NSTableView and explains why it appeared to randomly populate as it felt like it.
精彩评论