4

I have a sample app with single view controller. The controller's view has subviews look like following

UIView
    UIScrollView -- (Frame = {0, 0, 320, 480})
        UICollectionView -- (firstCollectionView, Tag = 0, Frame = {0, 0, 320, 480})
        UICollectionView -- (secondCollectionView, Tag = 1, Frame = {480, 0, 320, 480})
        UICollectionView -- (thirdCollectionView, Tag = 2, Frame = {960, 0, 320, 480})

Then, in viewDidload I call

[self.firstCollectionView reloadData]; // Tag = 0
[self.secondCollectionView reloadData]; // Tag = 1
[self.thirdCollectionView reloadData]; // Tag = 2

And in the delegate method, numberOfSectionsInCollectionView:, I put NSLog to see which collectionView start reloading its data first

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"Tag : %i", collectionView.tag);
}

Surprisingly, I got this result

2014-05-27 12:20:22.462 SampleApp[60652:60b] Tag : 2
2014-05-27 12:20:22.462 SampleApp[60652:60b] Tag : 1
2014-05-27 12:20:22.462 SampleApp[60652:60b] Tag : 0

Everytime I tried, it will result like this (2 -> 1 ->0) Do you have any idea why is the result (log) is reverse? Is it something to do with threading?

Best

P.S. I can't share the actual code, but, this is everything that related to the UICollectionView issue. I just call reloadData of multiple UICollectionView that is contained in a single UIScrollView in controller's viewDidLoad, and found this weird result.

Tar_Tw45
  • 3,122
  • 6
  • 35
  • 58

1 Answers1

1

The Reload calls run in separate threads once the reload call is made. The threads are not guaranteed to run sequentially. The framework would be asking for data on need basis. If the collection view is not in visible frame, then the delegate call back would not be fired at all.

If you are looking at sequentially reloading data, there are specific ways to achieve it. Please look into wait till UITableView finish reloadData and Get notified when UITableView has finished asking for data?. You can extend this for collection views as well.

Community
  • 1
  • 1
nprd
  • 1,942
  • 1
  • 13
  • 16
  • Thanks for the info, "reload run in separate thread". It making the picture more clear to me : ) – Tar_Tw45 May 27 '14 at 06:09
  • One question, if it's not guarantee that threads will run sequentially, why I got the same sequence every time I tried (2 -> 1 -> 0). Why there was no other sequence (1 --> 2 --> 0, 0 --> 2 --> 1 and else) – Tar_Tw45 May 27 '14 at 06:12
  • I am not sure why the sequence is always 2-->1-->0. A random guess would be the reload calls mitt get stacked up until the message execution completes. Is there any change if you change the sequence of reloads? like calling reload on 3 followed by 2 and then 1 or any other combination. – nprd May 27 '14 at 07:17
  • I'll try later and let you know. – Tar_Tw45 May 27 '14 at 07:51
  • Hey, @NaveenPrasadR. What do you mean by _**The framework would be asking for data on need basis. If the collection view is not in visible frame, then the delegate call back would not be fired at all.**_ That it won't reload when it's not visible? – Somoy Das Gupta Nov 15 '18 at 05:47