tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

handling a response (strange behaviour) #2713

Closed culexjj closed 8 months ago

culexjj commented 10 months ago

Hello

I'm having some issues managing the response of 'TdApi.SearchChatMessages'.

If I use this code it seems ok, I get the output printed on the console

...... private static FoundChatMessages var = new FoundChatMessages(); ...... private static void getChatMessages(final int limit, final long chat) {
client.send(new TdApi.SearchChatMessages(chat, "me imagino", null, 0, 0, 2, null, 0), new Client.ResultHandler() { @Override public void onResult(org.drinkless.tdlib.TdApi.Object object) { var = (FoundChatMessages) object; System.out.println ("\n" + var.toString()); } }); }

but I take the System.out.println ("\n" + var.toString()); sentence out like this, it doesn't print anything

private static void getChatMessages(final int limit, final long chat) {
client.send(new TdApi.SearchChatMessages(chat, "me imagino", null, 0, 0, 2, null, 0), new Client.ResultHandler() { @Override public void onResult(org.drinkless.tdlib.TdApi.Object object) { var = (FoundChatMessages) object; } }); System.out.println ("\n" + var.toString()); }

The most strange thing is if I debug the code it works fine, here you can see that var is not null.

2023-12-10 19_38_30-TFG - TDLibTest_src_org_drinkless_tdlib_example_Example java - Eclipse IDE

It is clear to me that I'm doing something wrong but I don't understand this behavior.

Can you help me?

Thanks in advance

Culex

levlam commented 10 months ago

You print the value of the variable var right after sending the request and long before it is initialized by the response handler.

culexjj commented 10 months ago

Hello Levlam,

Thanks for your reply, I think I understand what you meant. it is related with the way the client interac with Telegram servers "Fully-asynchronous. Requests to TDLib don't block each other or anything else, responses will be sent when they are available."

I get it :-)

Thanks

Culex.