1

我的TcpClient. 第一个是startListener,然后我做connect。他们俩都回来了void

在我当前的TcpClient实现中,如果我这样做,应用程序就会崩溃startListener,然后connect就在它之后(我想它们之间需要一段时间?)。实现在这里,来自SimpleTCPLibrary (他startListener在里面onStart(),并且有一个触发的连接按钮connect)。

我想做的是做startListener,什么时候以成功结束-> 做connect。我找不到任何示例来说明如何使用BoltsFramework continueWithonSuccess.

有没有例子?

4

2 回答 2

1
    Task.callInBackground(new Callable<Void>() { //or `Task.call` for synchronous
        @Override
        public Void call()
                throws Exception {
            /*... startListener */
            return null;
        }
    }).continueWithTask(new Continuation<Void, Task<Void>>() {
        @Override
        public Task<Void> then(Task<Void> ignored)
                throws Exception {
            return Task.delay(200);
        }
    }).continueWith(new Continuation<Void, Void>() {
        @Override
        public Void then(Task<Void> ignored)
                throws Exception {
            /*... connect */
            return null;
        }
    });

或使用 lambda:

Task.call(() -> { TcpClient.startListener(); return null; })
    .continueWithTask(ignored -> Task.delay(200))
    .continueWith(ignored -> { TcpClient.connect(); return null; });
于 2016-10-24T12:13:55.400 回答
1

你可以随时尝试

Task.delay(200).continueWith(new Continuation<Void, Object>() {
    @Override
    public Object then(Task<Void> task) throws Exception {
        ... connect();
        return null;
    }
});
于 2016-10-24T11:18:24.933 回答