iosreactive-swift

crash due to `Lock.UnfairLock.lock()` when using `UIRefreshControl` whilst observing `Action.isExecuting`


I'm using a UITableView to display a paginated list and I'm using a UIRefreshControl to indicate that a network request is taking place.

However, for some reason self.refreshControl.beginRefreshing()/self.refreshControl.endRefreshing() causes a crash on os_unfair_lock_lock(_lock). If I comment out both of these lines, the code works fine.

The following code can be used to reproduce the issue:

import UIKit
import ReactiveSwift
import Result

class ViewController: UIViewController {

    @IBOutlet var tableView : UITableView!
    var refreshControl : UIRefreshControl!

    var i : Int = 0
    var action : Action<Int,[String],NoError>!
    var words = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        refreshControl = UIRefreshControl()
        tableView.addSubview(refreshControl)

        action = Action<Int,[String],NoError>{ pageNumber in
            return SignalProducer{ sink,_ in
                let deadlineTime = DispatchTime.now() + .seconds(2)
                DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
                    if pageNumber == 1{
                        sink.send(value: ["Apple","Bananas","Clementines"])
                    }else if pageNumber == 2{
                        sink.send(value: ["Dodo","Eels","French"])
                    }
                    sink.sendCompleted()
                }
            }
        }

        action.values.observeValues { [weak self](moreWords) in
            if self?.words.isEmpty ?? true {
                self?.words = moreWords
            }else{
                self?.words.append(contentsOf: moreWords)
            }
            self?.tableView?.reloadData()
        }

         action.isExecuting.signal.observe(on: UIScheduler()).observeValues { [weak self](loading) in
            if loading{
                self?.refreshControl.beginRefreshing()
            }else{
                self?.refreshControl.endRefreshing()
            }
            print("loading \(loading)")
        }
    }
}

extension ViewController : UITableViewDataSource {

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return self.words.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let identifier = "Whatever"
        var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
        if cell == nil{
            cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
        }
        cell!.textLabel?.text = self.words[indexPath.row]
        return cell!
    }
}

extension ViewController : UITableViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (((scrollView.contentOffset.y + scrollView.frame.size.height) > scrollView.contentSize.height ) ){
           if !self.action.isExecuting.value {
                i += 1
                self.action.apply(i).start()
            }
        }
    }
}

Solution

  • Use the beginRefreshing & endRefreshing on main thread as:

    QueueScheduler.main {
        if loading {
            self?.refreshControl.beginRefreshing()
        } else {
            self?.refreshControl.endRefreshing()
        }
    }