NSOutlineView loads before ViewDidLoad ?

A NSTableView works fine I have a func which loads from a Json file, but using an NSOutlineView which is supposedly a version of Table View loads before a func loading a Plist file in either ViewDidLoad or ViewWillAppear ? My guess is due to it loading too slowley, although probably wrong as usual. The data does load but have to reload the table data.

Accepted Reply

There are many different ways to do it. I don't know anything about what you are doing, so I can't say what is the best method.


But generally, either you setup your data structures when you initialize the app or the document. Another option would be to update the UI dynamically as the data changes. But generally you would want to have the initial set of data loaded at first.

Replies

When oading is async. you should not assume any timing for completion.


You have to manage the consequences (if any) of some delay in your code.

I had wondered that but I don't think I'm using an async func ... NSArray(contentsOfFile: ...) as [NSDictionary] / .object(forKey: ...) ?

Just tested with a pre defined array and get different results ...

1) Blank array but fill in at ViewDidLoad

2) Array with 2 elements


2nd works fine but 1st has same problem. Hope that makes sense.

Have you checked this?


class NSOutlineView


Important

It is possible that your data source methods for populating the outline view may be called before

awakeFromNib()
if the data source is specified in Interface Builder. You should defend against this by having the data source’s
outlineView(_:numberOfChildrenOfItem:)
method return
0
for the number of items when the data source has not yet been configured. In
awakeFromNib()
, when the data source is initialized you should always call
reloadData()
.

How can I tell if the datasource is not yet configured ?

The data source is what you programmed. How can I tell without knowing any lines of your code?

Not sure what you're looking for or expecting to find but hope this helps ... http://www.extelligentcocoa.org/nsoutlineview-part1-setting-up-an-outlineview/https://www.raywenderlich.com/1201-nsoutlineview-on-macos-tutorial


I've posted this because it's easier than copying lots of text and images, it's what I used as a guide.

It's far better copying some hundred lines of YOUR code. I have no need to watch sort of tutorials.

Any method that has the name "did" means that it will be called after something else. Therefore, "viewDidLoad" means it will be called after the view has been loaded. That is too late to populate the data.

That's why I included ViewWillAppear.

OK. Look at the name of that method. Break it down into parts.


"view" - The thing we're talking about. In this case, the NSOutlineView object.

"will" - Temporal qualification. Something is going to happen in the future.

"appear" - The verb. This is what is happening, or in this case, going to happen.


In other words, this method says nothing about data. The documentation goes into more detail about what happens during this method. It also says nothing about data. Therefore, you can make no assumptions about data within this method.

I see your point. Is there another way to get it loaded at the point it needs to be ?

There are many different ways to do it. I don't know anything about what you are doing, so I can't say what is the best method.


But generally, either you setup your data structures when you initialize the app or the document. Another option would be to update the UI dynamically as the data changes. But generally you would want to have the initial set of data loaded at first.

I had a similar problem where the NSOutlineView reloads before ViewWillAppear somehwere in NSOutline's layout-code. It unfortunately only called some datasource functions like outlineView(_:child:ofItem):, but not outlineView(_:numberOfChildrenOfItem) which lead to inconsistent data access and crashes.

My solution was to not set the outlineview's datasource and delegate in the NIB-file, but to set them in ViewWillApear (and remove them in ViewDidDisappear).