MrAlek / PagedArray

A Swift data structure for easier pagination

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pageArray.loadedElements - Array index out of range when the last page is not fully loaded with elements

alejandrogarin opened this issue · comments

This is the test:

func testLoadedElementsForOnePageAndAHalfOfTheLastOne() {
var tinyArray: PagedArray = PagedArray(count: 15, pageSize: 10)
tinyArray.setElements(Array(0...9), page: 0)
tinyArray.setElements(Array(10...11), page: 1)
XCTAssertEqual(tinyArray.loadedElements.count, 12, "Array total count is not correct")
}

Thanks for setting up the issue.

I've been thinking about what to do with the last page, if it should be treated like all the others and require correct sizing or not. Since the array will mostly be used with a paging web service, the total count might differ from when you load the first page and you get the last.

What do you think, should I enforce correct sizing or make the array compensate on the last page itself?

I would not enforce the size in last page.
I came across this issue when adding a few methods to reorder, insert and delete elements in the array so I don't need to reload all the pages again.
I applied a local fix looping through the pages and collecting the elements.
I think that just returning the loaded elements avoiding the crash is good enough.

Sent from my iPhone

On May 11, 2015, at 4:49 PM, Alek Åström notifications@github.com wrote:

Thanks for setting up the issue.

I've been thinking about what to do with the last page, if it should be treated like all the others and require correct sizing or not. Since the array will mostly be used with a paging web service, the total count might differ from when you load the first page and you get the last.

What do you think, should I enforce correct sizing or make the array compensate on the last page itself?


Reply to this email directly or view it on GitHub.

I've taken a different take on this now (3c70011). In the 0.2 version, you have two options:

  1. Adjust the count property on the paged array before setting the mis-sized page.
  2. Set the updatesCountWhenSettingPages property to true, which removes all page size checking and dynamically adjusts the paged array to fit. That also makes infinite lists easy to implement since you can just append new pages.