Search code examples
swifttableviewcollectionviewpull-to-refreshdispatch-queue

Swift Pulltorefresh on tableview what might be causing Thread 1: EXC_BAD_ACCESS (code=1, address=0x53178d8ac) from provided code


When recently testing my pull to refresh function on my homepageviewcontroller after about 10-30 pulls to refresh sometimes the controller crashes with Thread 1: EXC_BAD_ACCESS (code=1, address=0x53178d8ac)

Here is my code in my pull to refresh function

//this function is for the tableview controllers pull to refresh
@objc func refreshWasPulled(){
    
    //check if this is for the trending screen
    if isRepresentingExplorePageClick == true {
        
        DispatchQueue.main.async {
            self.refreshControl.endRefreshing()
        }
        
    }else if isRepresentingFollowingData == false {
        //call the API
        HomepageAPInetwork.userTrendingContent(accessKey: accessKey, completionHandler: { posts in
            //set the posts
            self.followingPosts.removeAll()
            self.followingPosts = posts
            
            //call the main thread
            DispatchQueue.main.async {
                
                //reset the estimatedrowheight
                self.Tableview.estimatedRowHeight = 600
                //set the rowheight to use the automatic dimension
                self.Tableview.rowHeight = UITableView.automaticDimension
                
                self.refreshControl.endRefreshing()
                
                //reload the data
                //begin tableview updates
                self.Tableview.reloadData()
            }
            
        })
        
        
    }else if isRepresentingFollowingData == true {
        
        HomepageAPInetwork.userFollowingContent(accessKey: accessKey, completionHandler: {posts in
            self.followingPosts.removeAll()
            //set the posts
            self.followingPosts = posts
            
            //call the main thread
            DispatchQueue.main.async {
                
                //reset the estimatedrowheight
                self.Tableview.estimatedRowHeight = 600
                //set the rowheight to use the automatic dimension
                self.Tableview.rowHeight = UITableView.automaticDimension
                
                //end the refresh control
                self.refreshControl.endRefreshing()
                

                //reload the data
                self.Tableview.reloadData()
            }
            
        })
        
    }

Solution

  • Because i am using a custom Scrolltorow feature on my homepage it was actually messing up the refresh here is what i did to fix this issue.

    Before

    self.refreshControl.endRefreshing()
    

    After

    if self.refreshControl.isRefreshing == true {
       self.refreshControl.endRefreshing()
    }