pavankataria/SwiftDataTables

crash when DataSet / DataSource is empty

altagir opened this issue ยท 13 comments

Modify your sample to have empty data set

public func addDataSourceAfter(){
        self.dataSource = []
        self.dataTable.reload()
    }

2019-02-22 11:19:19.787461-0500 SwiftDataTables_Example[51841:24050067] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 22]'

Nice catch @altagir . I've had this happen to me before. Have you been able to look into this yet @altagir ? Would greatly appreciate an extra helping hand!

I tried but couldn't locate the exact cause yet, will dig further, but was perplex on that one.
I did circunvent issue for now by adding a single row alway with "no data" in first field

Yeah that's an option. It would be great if you could dig further. I think it might be to do with the process method rendering things when it doesn't need to.

Since I'm short for time I had to timebox this particular issue for this week.

I suspect it's to do with column widths having nan values in the array.
processColumnDataAverages

Indeed :D Found a fix !

in DataStructureModel.swift / processColumnDataAverages, replace :

columnContentAverages.append(Float(averageForCurrentColumn)/Float(data.count))

which creates indeed a division by 0 if data.count = 0, by :

columnContentAverages.append((data.count == 0) ? 100 : Float(averageForCurrentColumn)/Float(data.count))

100 being the default header size when content size couldn't be computed, could be a public parameter

or better yet, can the default header size be computed in this case byt size needed by each headers titles?

Nice one! Glad you were able to dig deeper since I had to go! My debugger was getting weird and kept referencing the incorrect lines!
So the point of the method is to find the average string length of the column for that value to be the width of the column header. I don't think it's the perfect solution since sometimes long headers titles will cut off if the column's average string length is say 2 for example. Then a column header width of that needs to accommodate a string size longer than 2 (unless the header name is PK = primary key ;p ) will have problems. Unless I set the automaticallyShrinksToFitWidth property on the label to true of course.

So I don't think setting 100 is the right way. I should check my method again and see if I can just have the columns width distribute proportionately across the frame. I have a method for that too...

I can't remember, but what happens visually when you set the size to 100? Does every single title in the demo project end up having a width of 100 regardless of column's title string size?

100 being the default header size when content size couldn't be computed, could be a public parameter
In short what I was trying to say was maybe this can be a header or an enum can be created which utilises my automatic scaling to find the appropriate width distribution for the header titles across the screen. It even goes beyond the screen if the algorithm (can't remember which method it was but it's in there) determines that the headers need more space so then it'll become a scroll data table.

an enum like this could be created:

enum ColumnWidthHeaderCalculation {
    case autoDistributeProportionately
    case fixedHeaderSizeWhenEmpty(_ size: CGFloat) 
} 

the names can do with some improvement.

FYI: adding this didn't resolve the crash for me. Did you add anything else? I'm still getting NaN values.

columnContentAverages.append((data.count == 0) ? 100 : Float(averageForCurrentColumn)/Float(data.count))

yes i confirm it works, was the only source of NaN
try :

rm -Rf ~/Library/Developer/Xcode/DerivedData/
and clean build

I had many discrepancies with code not doing so.

I changed to 1 for width, headers automatically fills the width
100 was making big width

columnContentAverages.append((data.count == 0) ? 1 : Float(averageForCurrentColumn) / Float(data.count))

I'm going to push a new version which will have this fixed. Will close this once that version is out
Thanks guys!

๐ŸŽ‰ ๐ŸŽ‰ @altagir
This is now available as of version 0.7.2 ๐ŸŽ‰ ๐ŸŽ‰ https://github.com/pavankataria/SwiftDataTables/releases/tag/0.7.2

Let me know what you think ๐ŸŽ‰

๐Ÿš€ SwiftDataTables (0.7.2) successfully published on ๐Ÿ“… March 7th, 22:58 GMT
๐Ÿ‘ Share with your friends and colleagues!