1

I've already found in SO several answers on how to pre-load all tab views when app launches with either this solution here or here but nothing seems to work.

With both of these solutions, second tab view is clearly loaded from first tab however (simple print checking) however I've noticed collectionView datasource methods are not called. I want to avoid the slight delay before photos are populated into collection view, this is why I want to load second tab when app launches.

It clearly seems that assets are fetched for the first time (in ViewDidLoad method) when second tab view appears and not before when instantiated in first tab as requested.

Any ideas ?

thanks.

Julm
  • 25
  • 7
  • You will need to analyze the situation a bit more or extract your code to create a minimum that is needed to reproduce your issue. Many things may have gotten wrong and members here may only keep shooting in the dark like "are all your cell at index path being called at view did load", "have you checked that when the new controller appears it is indeed by pointer confirmed that it is the same object"... – Matic Oblak Mar 23 '18 at 08:13
  • well i've just checked and both collectionview datasource methods 1/number of items in section and 2/cellforItem are not called. – Julm Mar 23 '18 at 08:32
  • So you might want to call `reloadData` on collection view. If you already do that also check its frame. If view is too small it will not display cells and will not call those methods for it. Try resizing the view controller or something I guess. – Matic Oblak Mar 23 '18 at 08:41
  • I'm already calling reloadData, still not working and checked frames as well and they are correctly sized. So basically now the question would be why collectionView datasource methods aren't called.. – Julm Mar 23 '18 at 08:49
  • Well is its `dataSource` non-null and valid? At least some `numberOff` method should be called then so the next is checking if you return non-zero values on it. – Matic Oblak Mar 23 '18 at 08:59
  • I'll try this, how do you implement this method? thanks – Julm Mar 23 '18 at 09:01
  • There are 2 methods `numberOfItemsInSection` and `numberOfSections` in data source. You will probably just need to put some breakpoint in them... – Matic Oblak Mar 23 '18 at 09:07
  • both of these methods are not called at all, the variable from where the data is taken is definitely not empty but neither of these datasource methods are called. – Julm Mar 23 '18 at 09:15
  • Than at the point you call `reloadData` I assume that `collectionView.dataSource` is `null`. – Matic Oblak Mar 23 '18 at 09:19
  • well any ideas how to solve that? – Julm Mar 23 '18 at 10:00
  • Calling `collectionView.dataSource = self` before calling `collectionView.reloadData()` should be enough. – Matic Oblak Mar 23 '18 at 10:01
  • it's already done, the problem is not here I suppose because collectionView is correctly populated when second tab appears. – Julm Mar 23 '18 at 10:03
  • Did you solve it? Having the exact same issue – Roi Mulia Apr 03 '19 at 11:11

1 Answers1

0

try this

(self.tabBarController?.viewControllers?[3] as? UINavigationController)?.viewControllers[0].viewWillAppear(true)

Change 3 to the index of the designated tab bar.

In the designated view controller put your reloading code in the viewwillappear such as

override func viewWillAppear(_ animated: Bool) {
    myCollectionView.reloadData()
}
Omar N Shamali
  • 503
  • 5
  • 11