简体   繁体   English

Kotlin高阶函数传递一个带有可变数量参数的函数作为参数

[英]Kotlin Higher Order Function pass a function with variable number of arguments as the parameter

In my application I need to perform Network calls using a specific framework. 在我的应用程序中,我需要使用特定的框架执行网络调用。 Because every network call need to be performed on the separate thread I would like to have one function which start new Thread perform a call and return an object. 因为每个网络调用都需要在单独的线程上执行,所以我希望有一个函数启动新线程来执行调用并返回一个对象。 To do so I tried to use HigherOrderFunctions, but I didn't find until now how to declare function as an argument which take a variable number of arguments. 为此,我尝试使用HigherOrderFunctions,但直到现在我才发现如何将function声明为带有可变数量参数的参数。

To give you an idea I would like to have something like this: 为了给您一个主意,我想提供以下内容:

    fun  Client.performNetworkCall(calledFunction:(vararg Object)->Object):Object{
Thread(Runnable {
  calledFunction
}).start()

 //return function results

}

But it seems to impossible to declare such function. 但是似乎无法声明这样的功能。 Is it possible in Kotlin? 在科特林有可能吗? I would like avoid creating every time new thread in my code when I need to perform a network call. 我想避免在需要执行网络调用时在代码中每次创建新线程。 So that I can write something like this 这样我就可以写这样的东西

client.performNetworkCall{ bean.createNewUser(User("","","Gosia","gosiak@gmail.com","pass"))}

bean is object of my interface which hava a function createNewUser. bean是我的接口的对象,它具有函数createNewUser。 Function createNewUser is implement on server and will return some result after execution. 函数createNewUser是在服务器上实现的,执行后将返回一些结果。

If what I want to do is not possible using higher order function, can you give me a hint what else can I do get something like I described above? 如果使用高阶函数无法完成我想做的事情,您能否给我一个提示,我还能做些如上所述的事情?

Whatever you are asking is against the Android best practices. 无论您要问什么,都违反了Android最佳做法。 May be mistakenly you have mentioned it in question. 您可能会错误地提到了它。 You want a function to execute Higher order function in a background thread and (pay attention here) return the value immediately (means block the calling thread until background task is completed). 您想要一个函数在后台线程中执行高阶函数 ,并(请注意此处)立即返回该值(意味着阻塞调用线程,直到后台任务完成)。

As you must be knowing that network call should be async call and for that you have to pass an interface to the async method as parameter so that you can be notified when your Higher order function completed it's execution. 您必须知道网络调用应该是异步调用,并且为此您必须将接口传递给async方法作为参数,以便可以在高级函数完成执行时收到通知。

And the better option is to use Coroutines a really good alternative for scheduling task in background. 更好的选择是使用程作为在后台调度任务的一种非常好的选择。

So your given code can be converted like: 因此,您可以将给定的代码转换为:

async {
    var result = bean.createNewUser(User("","","Gosia","gosiak@gmail.com","pass"))
    // do whatever you want to do with result here        
}

for using Coroutines, you have to add following line in module level gradle file: 要使用协程,您必须在模块级gradle文件中添加以下行:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21.2'

Note: Coroutines are in experimental phase now, it might change. 注意:协程目前处于实验阶段,可能会更改。

Another option using doAsync from kotlin anko library that is fairly simple and easy to use. 来自kotlin anko库的使用doAsync的另一种选择是相当简单且易于使用。

Example: 例:

doAsync {
        var result = bean.createNewUser(User("","","Gosia","gosiak@gmail.com","pass"))
        // use result here to to further background task

        uiThread {
            // use result here if you want to update ui
        }
    }

for using doAsync add following line in module level gradle file: 要使用doAsync,请在模块级gradle文件中添加以下行:

compile "org.jetbrains.anko:anko-commons:0.10.0"

To read more for doAsync click here . 要了解更多关于doAsync的信息,请单击此处

Hope it helps. 希望能帮助到你。 Let me know if you have further questions. 如果您还有其他问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM