Skip to content

Instantly share code, notes, and snippets.

@andr-ggn
Created October 25, 2016 14:07
Show Gist options
  • Save andr-ggn/0e3e1c3f56cfa07a66b0d8c4418bf6c4 to your computer and use it in GitHub Desktop.
Save andr-ggn/0e3e1c3f56cfa07a66b0d8c4418bf6c4 to your computer and use it in GitHub Desktop.
CATransaction.begin()
                    CATransaction.setDisableActions(true)
                    
                    let oldBottomOffset = self.collectionView.contentSize.height - self.collectionView.contentOffset.y
                    
                    self.collectionView.performBatchUpdates({
                        // indexPaths for earlier messages
                        let lastIdx = history.count - 1
                        //let lastIdx = self.messagesCountIndexes
                        var indexPaths: [AnyObject] = []
                        for i in 0...lastIdx {
                            indexPaths.append(NSIndexPath(forItem: i, inSection: 0))
                        }
                        
                        //Convert the messages in modelbridge
                        var messages = [ModelMessageBridge]()
                        
                        for message in history {
                            messages.append(ModelMessageBridge(message: message))
                        }
                        
                        // insert messages and update data source.
                        
                        for message in messages {
                            self.JSQmessages.insert(message, atIndex: 0)
                        }
                        self.collectionView.insertItemsAtIndexPaths(indexPaths)
                        
                        // invalidate layout
                        self.collectionView.collectionViewLayout.invalidateLayoutWithContext(JSQMessagesCollectionViewFlowLayoutInvalidationContext())
                        
                        println("*** inserted \(messages.count) messages")
                        }, completion: {(finished) in
                            
                            //scroll back to current position
                            self.finishReceivingMessageAnimated(false)
                            self.collectionView.layoutIfNeeded()
                            self.collectionView.contentOffset = CGPointMake(0, self.collectionView.contentSize.height - oldBottomOffset)
                            CATransaction.commit()
                            
                            self.showLoadEarlierMessagesHeader = true
                    })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment