URLConnection: Send or SendSync?

For a web service app using POST and GET, which do you prefer using: Send or SendSync?

If I am reading the docs correctly, SendSync can make the app unresponsive. I have noticed this a tad. Is there any benefit using SendSync over Send or vice versa?

Are you asking about using URLConnection to access your web service? Or your web service using URLConnection to access something else on the web?

The connection to access the web service from a desktop app

I prefer Send. It forces you to think in an event-driven paradigm, but it keeps the UI responsive.

SendSync can be useful for short, discrete calls, and allows you to write sequential, procedural code. That has its place. The problem with SendSync is you don’t notice the lag while you’re testing locally. You can have great code that is very responsive - until you go into production on the internet. And then everything unravels and you get a jerky, unresponsive UI.

One issue with Send is if you have multiple steps to complete, you get into the situation where in ContentReceived you find yourself wondering, Why did I come into this room? There are several ways to solve that problem, though.

2 Likes

This is helpful Tim. Thank you for sharing your expertise. I coded everything with SendSync initially. I have noticed, even with a simple POST, that there is a micro stall. This is in the built web service, not running locally.

I have some other calls that will require lengthier processing (posting via a loop) where a Send might be better suited. Haven’t tested these yet, just wrote them out. Taking your words of caution, I’ll try to work these out as they will have multiple steps

Does a thread help with unresponsiveness with SendSync, or should I not bother and just plan to use the thread with Send for the loop POSTs?

Unfortunately, a thread will not help.

Let me amend that. If you have a sequence of SendSync calls in a thread, They may stall the UI for the cumulative time they take. You can mitigate that by calling YieldToNext after each SendSync. Any single SendSync will lock the UI, even in a thread, but you don’t have to wait for them all to finish.

1 Like