2

I know how to implement pagination with UITableview but my question is we always append data of next page with existing complete data array so every next page array is increasing array size.

For example - We get 50 records in first page and we request for next page and we again get 50 records and then we will append that records in existing complete array so complete array is now having 100 records. I am requesting data with around 100 pages so my array will have 5000 records as we know holding some starting page array data is not good idea as we hardly come back for starting page after visited 100 pages .

Is there any way to optimize array size? please help me on this as i searched a lot but didn't find good answer for this.

I would be very grateful for help and sorry for my bad english.

5
  • You can use server side. take a look here: medium.com/apps-geeks-community/… Commented Oct 7, 2018 at 13:23
  • we need to create two array one is server data array and another one is old array data when server send you second page data you just need to add in old array. Commented Oct 7, 2018 at 13:23
  • Please check this may it will help you : stackoverflow.com/questions/35367496/swift-tableview-pagination Commented Oct 7, 2018 at 13:25
  • @MaheshShahane approved answer is removing all items before fetch next data what if we scroll up after come to second page and it will again fetch first page data, so i think this will not be a good solution. Commented Oct 7, 2018 at 13:34
  • This will work for me stackoverflow.com/questions/35367496/… check this and let me know. Commented Oct 7, 2018 at 13:44

1 Answer 1

2

I think you can achieve that by writing the "old" data to a local storage, and retrieve and insert back into your array.

So, imagine that you've already fetched, lets say 200 items. So when the user scrolls down, and you fetched the next page (the next 20 items), you "cut" from your array the items from 0 to 99 and write to a file. Now your array has 120 items. Then, when the user continues scrolling and again reached 220 (array.count >= 220), repeat the same logic, and so on.
Now the most interesting part. If the user scrolls back and the index of the top visible cell is <100, you read the previously written data from the file (and remove from the file) and insert into your array at 0 position.
And of course it'd be better to clear all that kind of files on the app launch.

Of course the numbers I wrote below are magic numbers and you should play with them to find the right ones that best fit your needs.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.