2

Pre-requisites - Environment: iOS 9.0 or above - using Swift 3.0.1

Thanks for your responses. I'm updating the question and trying to give a better understanding about the problem.

Posting code would help may be but I'm not allowed to post the code as I do not have the IP.

But I am trying to build something like calendar/program guide where you have events for each category for several days.

Imagine, categories on your left side in a column and they can be the sections of the collectionveiw and each category has events for several days which is a row.

CAT 1 : Event 1, Event 2 ... Event n CAT 2 : Event 1, Event 2 ... Event n CAT 3 : Event 1, Event 2 ... Event n . . . CAT m : Event 1, Event 2 ... Event n

Problem: The entire data is pretty dynamic and humongous. I can't prefetch all the records, they are about over 80-100K. It takes few minutes to download all the data and display it on the grid.

A user could select any day and any time and I have to scroll the collection view to that day and time and display those events for the categories. Also, user could obviously scroll in both directions to and browse the events in this case the events are loaded like infinite scroll fashion.

In the former option though, when the user jumps on to a particular day and time on the entire timeline and I have to skip loading the other previous events (as I do not have them yet - unknown) and display the events relevant to the user selected days and time.

I do not have all the IndexPaths in advance, to display on the screen, how can I skip events and dynamically update the collection view in parts like we load images dynamically and the ones which get loaded first and displayed earlier than others.

I'm using startDate of the events to calculate the xPosition, categories don't change often after they are loaded so we could somehow avoid reloading sections but items in those sections change all the time and they appear in a random fashion.

When the controller loads the first set of events are fetched from the server and displayed, now if the user decided to jump to some D-Day and T-Time which could be anywhere on the entire timeline I have to fetch the events for those dates and populate the items for relevant sections (visible on screen) and update the interface. This is where I have issues, where I do not have an proper approach.

Hope this is clearer. I have "tried" to mock this up

hdawg
  • 21
  • 5
  • It will be easier if you show the codes you are currently using to calculate your xPosition and that you use to populate the collectionView. As far as I have tried, with the data used to populate the collection view, it is always possible to calculate the indexPath of a specific item you are looking for. – Ben Ong Dec 09 '16 at 05:37
  • Or by your current situation, you might want to instead try to split your collection view into sections by days or months whichever more relevant and that might make calculating the position of your desired cell easier – Ben Ong Dec 09 '16 at 05:39
  • If you implement robust **lazy loading** based on index path, you can simply use `scrollToItem(at:at:animated:)` ( [reference](https://developer.apple.com/reference/uikit/uicollectionview/1618046-scrolltoitem) ) to scroll the collection view to relevant index path. – Swapnil Luktuke Dec 09 '16 at 05:58
  • @BenOng Thanks for your response. I'm sorry but I'm not allowed to post the code as I do not have the IP. But I am trying to build something like calendar where you have events for each day. Imagine, Days on your left side in a column and they can be the sections of the collectionveiw and each day has events which is a row. Day 1 : Event 1, Event 2 ... Event n Day 2 : Event 1, Event 2 ... Event n Day 3 : Event 1, Event 2 ... Event n . . . Day n : Event 1, Event 2 ... Event n Problem: The entire data is pretty dynamic and humongous. I can't prefetch all the records. Cont 1/2.. – hdawg Dec 10 '16 at 07:13
  • @BenOng (Cont 2/2) - I have like 100K events to display and a user could select any day and any time and I have to scroll the collection view to that day and time and display those events. Now user could obviously scroll in both directions to and browse the events in this case the events are loaded like infinite scroll fashion. The other option though, he could jump on to a particular day and time on the entire timeline and I have to skip loading the other events (as I do not have them yet - unknown) and display the events relevant to the user selected days and time. I hope this is more clear? – hdawg Dec 10 '16 at 07:19
  • @BenOng - I have updated the question. – hdawg Dec 10 '16 at 07:54
  • In this case it will be easier to do it on the server side, to pass an index of the search result so that you can scroll to the correct indexPath. If that is impossible, you can try to split into multiple screens, each showing only an amount of data small enough to download(say one month?) and discard once the user moves on to the next. – Ben Ong Dec 12 '16 at 01:39

2 Answers2

0

UICollectionViewFlowLayout can help you achieve what you want...

https://developer.apple.com/reference/uikit/uicollectionviewflowlayout

https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html

eNeF
  • 3,241
  • 2
  • 18
  • 41
  • Wont work. I have updated the question, hope that would give you a better understand of the problem – hdawg Dec 10 '16 at 07:55
0

You have the same problem I had with my calendar project. The solution I have implemented will not work for you, but I am mentioning it here so that it might give you clues on how to solve it for your situation.

My calendar has a function where a user can scroll to some date way into the future. The problem was that date cells can be custom sizes. Therefore, since they are scrolling to some future date, in order for me to know the destination offset, I needed to know the offsets of cells 0 -to- destinationOffset because the cell sizes are different. This meant I had to query the sizes of all the cells in the middle which led to a 2-3 lag time (or in your case, a long download time).

So here was my solution. I originally had a delegate function called sizeForCellsAtMonth which was called for every month in order to determine the size. I have now changed this function to be called only once.

The function now only has two parameters:

  1. defaultSizeOfCells
  2. exceptionToDefaults - this will be specific months where the cell sizes are different

Using this information, I can calculate the sizes of all months because I know the sizes before hand. So my problem was solved by changing the way I looked at my delegate. Maybe you can try looking somewhere along those lines or maybe my answer gave you clues of what you can do.

Just a coder
  • 15,480
  • 16
  • 85
  • 138