0

Work with Telegram JNI Lib. Want to call getChatList() method and wait while TdApi.GetChats() returns result.

The current implementation of the method is as follows:

  1. Send a request to get chats in Telegram JNI Lib:

    client.send(new TdApi.GetChats (offsetOrder, offsetChatId, limit - chatList.size ()), new Client.ResultHandler()
  2. When it complete, recursively call getChatList() method and print the result.

How i can call getChatList() method and wait until it executes?

private static void getChatList(final int limit) {
    synchronized (chatList) {
        if (!haveFullChatList && limit > chatList.size()) {
            // have enough chats in the chat list or chat list is too small
            long offsetOrder = Long.MAX_VALUE;
            long offsetChatId = 0;
            if (!chatList.isEmpty()) {
                OrderedChat last = chatList.last();
                offsetOrder = last.order;
                offsetChatId = last.chatId;
            }
            client.send(new TdApi.GetChats(offsetOrder, offsetChatId, limit - chatList.size()), new Client.ResultHandler() {
                @Override
                public void onResult(TdApi.Object object) {
                    switch (object.getConstructor()) {
                        case TdApi.Error.CONSTRUCTOR:
                            System.err.println("Receive an error for GetChats:" + newLine + object);
                            break;
                        case TdApi.Chats.CONSTRUCTOR:
                            long[] chatIds = ((TdApi.Chats) object).chatIds;
                            if (chatIds.length == 0) {
                                synchronized (chatList) {
                                    haveFullChatList = true;
                                }
                            }
                            // chats had already been received through updates, let's retry request
                            getChatList(limit);
                            break;
                        default:
                            System.err.println("Receive wrong response from TDLib:" + newLine + object);
                    }
                }
            });
            return;
        }

        // have enough chats in the chat list to answer request
        java.util.Iterator<OrderedChat> iter = chatList.iterator();
        System.out.println();
        System.out.println("First " + limit + " chat(s) out of " + chatList.size() + " known chat(s):");
        for (int i = 0; i < limit; i++) {
            long chatId = iter.next().chatId;
            TdApi.Chat chat = chats.get(chatId);
            synchronized (chat) {
                System.out.println(chatId + ": " + chat.title);
            }
        }
    }
}
  • If you truly wanted to pause the thread until the method call is complete, I think that using monitors and `Object.wait()` would be useful. See this SO post: https://stackoverflow.com/questions/3362303 – Lucas Baizer Feb 13 '18 at 19:10
  • You can use a `CountDownLatch` object and share it with both client and main thread: On the client side, do `latch.countDown()` every time you've received and finished printing a new result; On the main thread side, do `latch.await()` to wait for the latch to be 0. See https://stackoverflow.com/a/289567/4381330 – Mincong Huang Jun 22 '18 at 13:26

0 Answers0