MrAlek / PagedArray

A Swift data structure for easier pagination

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Index out of bounds

kjoneandrei opened this issue · comments

commented

I have recently encountered several crashes related to this function

/// Accesses and sets elements for a given flat index position.
    /// Currently, setter can only be used to replace non-optional values.
    public subscript (position: Index) -> T? {
        get {
            let pageIndex = page(for: position)
            
            if let page = elements[pageIndex] {
                return page[position%pageSize]
            } else {
                // Return nil for all pages that haven't been set yet
                return nil
            }
        }
        
        set(newValue) {
            guard let newValue = newValue else { return }
            
            let pageIndex = page(for: position)
            var elementPage = elements[pageIndex]
            elementPage?[position % pageSize] = newValue
            elements[pageIndex] = elementPage
        }
    }

specifically this line return page[position%pageSize] throws IndexOutOfBounds errors

Version used: 0.8

I have the same issue, it happens after calling self.dataSourceArray?.set(data, forPage: page) in my view controller where dataSourceArray is a PagedArray.

There a fix in this fork

            let elementIndex = position%pageSize
            
            if let page = elements[pageIndex], elementIndex < page.count {
                return page[position%pageSize]
            } else {
                // Return nil for all pages that haven't been set yet
                return nil
            }

@kjoneandrei does this work for you too?

Even with your fix I still get the crash when I load a long list then a short one and scroll to the end of the short one.

commented

I have the same issue, it happens after calling self.dataSourceArray?.set(data, forPage: page) in my view controller where dataSourceArray is a PagedArray.

There a fix in this fork

            let elementIndex = position%pageSize
            
            if let page = elements[pageIndex], elementIndex < page.count {
                return page[position%pageSize]
            } else {
                // Return nil for all pages that haven't been set yet
                return nil
            }

@kjoneandrei does this work for you too?

@petard sorry for the months of wait for my reply. I am unable to test the specific case again as I don't maintain the project anymore, but I ended up going with own custom solution for this.

My issue is solved and was due to another part of the program.

Sorry for super late activity. Back again because of Hacktoberfest :)

Just as a regular array crashes for indexes out of bounds, the PagedArray also does. This only happens if when setting a page, the page size mismatches what the paged array's page size setting is. In that case, the entire paged array should be invalidated since data has changed and indexes can no longer be trusted. Closing.