在主线程上运行关闭

在主线程上运行关闭

本文介绍了在主线程上运行关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个类比使开发人员更容易实现选择器视图更为重要.在UIPickerView数据源方法中,我从闭包中返回值.示例:

I created a class than makes implementing a picker view easier for the developer. In the UIPickerView Datasource methods, I'm returning the values from a closure. Example:

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerNumberOfRows!(component: component)
}

我对所有数据源方法都使用相同的方法.当我运行该应用程序时,它在屏幕上显示没有数据的选择器.问题是我正在从闭包中接收数据,而闭包又不在主线程上运行.

I am using the same thing for all datasource methods. When I run the app, it shows the picker on screen with no data. The problem is I'm receiving data from the closure which in turn are not running on the main thread.

有没有一种方法可以在主线程上运行该闭包,并确保它返回适合于数据源函数返回值类型的值? (例如,在上面的示例中,闭包将返回一个Int)

Is there a way to run that closure on the main thread and make sure it returns a value suitable for the datasource function return value type? (ex, from the example above, the closure will return an Int)

我知道要使用dispatch_async.但是,就我而言,这无济于事.假设我创建了一个在主线程上运行闭包并需要返回值的函数,它看起来像这样:

I'm aware of using dispatch_async. However, in my case it will not help me. Let's say I created a function that runs the closure on the main thread and needs to return the value it will look something like this:

func closureOnMainThread()->Int{
    dispatch_async(dispatch_get_main_queue()) {
        // Run your code here
        return self.pickerNumberOfRows!(component: 0)
    }
    return 0
}

但是,在上面的函数中存在一个问题,该问题在返回调度块内部的值之前返回0.

However, There is a problem in the function above which is returning 0 before return the value inside the dispatch block.

修改2:弄清楚.我的代码包含一个用于选择器的自定义类和一个实现它的UIViewController:

Edit 2:To make things clear. My code contains a custom class for the picker and a UIViewController implementing it:

class CustomPickerView: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {

        var pickerNumberOfRows: ((component: Int)->Int)?

        //MARK: UIPickerViewDatasource Methods
        func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return pickerNumberOfRows!(component: component)
        }

        func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
            return pickerNumberOfComponents!()
        }

        func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {print("Hello")
            return pickerRowTitle!(forRow: row, inComponent: component)
        }

        //MARK: UIPickerViewDelegate Methods
        func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            pickerDidSelect!(row: row, component: component)
        }
    }

UIViewController(查看已加载):

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let picker = CustomPickerView(parent: self)

        picker.pickerNumberOfRows = { component in
            return 10
        }
    }

请注意,我删除了介于两者之间的附件功能,以便您专注于主要代码部分.

Please note I removed allot of accessory functions in between so that you focus on the main code part.

开发人员可以在闭包变量中传递他想要的逻辑.

The developer can pass in the logic he wants in variables of closures.

推荐答案

希望我目前无法理解.在此块中,您可以运行任何代码,它将在主线程上执行.

Hope I undestand currently. In this block you can run any code, and it will execute on the main Thread.

dispatch_async(dispatch_get_main_queue()) {
            // Run your code here
        }

您可以运行此命令以了解您是否在主线程上:

You can run this to know if you are on main thread or not:

NSThread.isMainThread()

编辑

您正在寻找的是要返回同步响应的内容:->整数

EDIT

What you are looking for is, insted of returning a sync response :->Int

您应该返回一个块,异步响应:

You should return a block, async response:

func someLongProcessTaskAndReturnCompenents(completion: (components: Int)->Void){

    //Here you can do your long process task on back thread...
                    dispatch_async(dispatch_get_main_queue()) {

                        completion(self.pickerNumberOfRows!(component: 0))
                    }
         }

现在,当您运行它时,响应将返回同步:

Now when you run it, the response will return synchronized:

someLongProcessTaskAndReturnCompenents { (components) in
            //Your logic after response
        }

这篇关于在主线程上运行关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:04