CollectionView flash when reloadData

I have a collectionView which is populated with around 60 images downloading from parse. These images can be updated depending if any new ones have been uploaded.

But my problem is after I load the view, and I refresh the data using PullToRefresh function, the collection view Flashes white and then displays the images again…

  • Objective-C, cancel a dispatch queue using UI event
  • Objective-C IF statement with OR condition
  • Give a top inset to UIRefreshControl
  • Is there an easy way to convert Android Application to IPad, IPhone
  • Tradeoff between LaTex, MathML, and XHTMLMathML in an iOS app?
  • When to use instance variables and when to use properties
  • here’s a video to show you :

    https://www.youtube.com/watch?v=qizaAbUnzYQ&feature=youtu.be

    I have been trying to fix this all day & find a solution, but I have had no success..!

    Heres how I’m querying the images :

     func loadPosts() {
         self.activityView.startAnimating()
            let followQuery = PFQuery(className: "Follows")
            followQuery.whereKey("follower", equalTo: PFUser.currentUser()!.username!)
            followQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
                        if error == nil {
                            self.followArray.removeAll(keepCapacity: false)
    
                            for object in objects! {
                                self.followArray.append(object.valueForKey("following") as! String)
    
                            }
    
            let query = PFQuery(className: "Posts")
            query.limit = self.page
            query.whereKey("username", notContainedIn: self.followArray)
            query.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)
            query.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
    
                if error == nil {
                    self.postImage.removeAll(keepCapacity: false)
                    self.uuidArray.removeAll(keepCapacity: false)
                    self.usernameArray.removeAll(keepCapacity: false)
                    for object in objects! {
                        self.postImage.append(object.valueForKey("image") as! PFFile)
                        self.uuidArray.append(object.valueForKey("uuid") as! String)
                        self.usernameArray.append(object.valueForKey("username") as! String)
                    }
    
    
    
                } else {
                    print(error!.localizedDescription)
                }
                self.collectionView.reloadData()
                self.refresher.endRefreshing()
                self.activityView.stopAnimating()
                self.boxView.removeFromSuperview()
    
    
            })
    
               }
                        })
        }
    

    And here is how I am pulling to refresh:

    override func viewDidLoad() {
            super.viewDidLoad()
    
            refresher.addTarget(self, action: "reload", forControlEvents: UIControlEvents.ValueChanged)
            collectionView.addSubview(refresher)
            loadPosts()
    
        }
    
    
        func reload() {
    
            collectionView.reloadData()
            refresher.endRefreshing()
    
    
        }
    

    Solutions Collect From Internet About “CollectionView flash when reloadData”

    I assume that the UUID’s are unique for every post, so you can check to see if the count from the previous load is different from the current, then you can see which posts are new, figure out their index path then only reload those index paths. I used sets to determine which id’s had been added, which will work assuming you don’t want to display the same post twice. There might be a better way of doing it, but in general you need to do something similar to the following:

     func loadPosts() {
             self.activityView.startAnimating()
                let followQuery = PFQuery(className: "Follows")
                followQuery.whereKey("follower", equalTo: PFUser.currentUser()!.username!)
                followQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
                            if error == nil {
                                self.followArray.removeAll(keepCapacity: false)
    
                                for object in objects! {
                                    self.followArray.append(object.valueForKey("following") as! String)
    
                                }
    
                let query = PFQuery(className: "Posts")
                query.limit = self.page
                query.whereKey("username", notContainedIn: self.followArray)
                query.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)
                query.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
    
                    if error == nil {
                        let oldUUIDArray = self.uuidArray
                        self.postImage.removeAll(keepCapacity: false)
                        self.uuidArray.removeAll(keepCapacity: false)
                        self.usernameArray.removeAll(keepCapacity: false)
                        for object in objects! {
                            self.postImage.append(object.valueForKey("image") as! PFFile)
                            self.uuidArray.append(object.valueForKey("uuid") as! String)
                            self.usernameArray.append(object.valueForKey("username") as! String)
                        }
                        let uuidOldSet = Set(oldUUIDArray)
                        let uuidNewSet = Set(self.uuidArray)
                        let missingUUIDs = uuidNewSet.subtract(uuidOldSet)
                        let missingUUIDArray = Array(missingUUIDs)
                        let missingUUIDIndexPaths = missingUUIDArray.map{NSIndexPath(forItem:self.uuidArray.indexOf($0)!,inSe ction:0)} 
    
                        let extraUUIDs = uuidOldSet.subtract(uuidNewSet)
                        let extraUUIDArray = Array(extraUUIDs)
                        let extraUUIDIndexPaths = extraUUIDArray.map{NSIndexPath(forItem:oldUUIDArray.indexOf($0)!,inSection:0)}
                        self.collectionView.performBatchUpdates({
                        if extraUUIDIndexPath != nil {
                            self.collectionView.deleteItemsAtIndexPaths(extraUUIDIndexPaths)
                        }
                        if missingUUIDIndexPaths != nil {self.collectionView.insertItemsAtIndexPaths(missingUUIDIndexPaths)}
                        }, completion: nil)
    
    
                        } else {
                            print(error!.localizedDescription)
                        }
    
                    self.refresher.endRefreshing()
                    self.activityView.stopAnimating()
                    self.boxView.removeFromSuperview()
    
    
                })
    
                   }
                            })
            }
    
        func reload() {
    
                self.loadPosts()
                refresher.endRefreshing()
    
    
            }