@@ -60,24 +60,19 @@ class FIRChatViewController: UIViewController, UICollectionViewDelegateFlowLayou
6060 self . authStateListenerHandle = self . auth? . addStateDidChangeListener { ( auth, user) in
6161 self . user = user
6262 self . query = self . chatReference. queryLimited ( toLast: 50 )
63-
64- // The initializer called below--though it takes a collection view--
65- // doesn't actually set the collection view's data source, so if
66- // we don't set it before trying to populate our view our app will crash.
67- self . collectionViewDataSource = FirebaseCollectionViewDataSource ( query: self . query!,
68- prototypeReuseIdentifier: FIRChatViewController . reuseIdentifier,
69- view: self . collectionView)
70- self . collectionView. dataSource = self . collectionViewDataSource
71-
72- self . collectionViewDataSource. populateCell { ( anyCell, data) in
73- guard let cell = anyCell as? FIRChatCollectionViewCell else {
74- fatalError ( " Unexpected collection view cell class \( anyCell. self) " )
75- }
76-
77- let chat = Chat ( snapshot: data as! FIRDataSnapshot ) !
63+
64+ self . collectionViewDataSource =
65+ FirebaseCollectionViewDataSource ( query: self . query!,
66+ view: self . collectionView,
67+ populateCell: { ( view, indexPath, snap) -> UICollectionViewCell in
68+ let cell = view. dequeueReusableCell ( withReuseIdentifier: FIRChatViewController . reuseIdentifier,
69+ for: indexPath) as! FIRChatCollectionViewCell
70+ let chat = Chat ( snapshot: snap) !
7871 cell. populateCellWithChat ( chat, user: self . user, maxWidth: self . view. frame. size. width)
79- }
80-
72+ return cell
73+ } )
74+ self . collectionView. dataSource = self . collectionViewDataSource
75+
8176 // FirebaseArray has a delegate method `childAdded` that could be used here,
8277 // but unfortunately FirebaseCollectionViewDataSource uses the FirebaseArray
8378 // delegate methods to update its own internal state, so in order to scroll
0 commit comments