Nominate our 2022 Qt Champions!

Proper socket handling for iOS tablets

  • I have an iOS application written with Qt. The main application uses a QThread for socket communications with several connected signals to/from the main application thread. Realizing that under some circumstances the OS may take my connection away, such as when the table sleeps or my app is forced to the background, what is the best or proper way to:

    • deal with the application state change with respect to my connection
    • restore the connection when the application returns to the foreground

    Do I need to disconnect my signals reflexively when the application state changes to SUSPENDED and then reconnect them when the state changes back to the foreground?

    Can I just leave everything alone and check the connection state when I next try to send a message, and reconnect if connection is lost?

    I appreciate any insight.

  • Lifetime Qt Champion


    Do you need to handle that use case or rather have your application run in background to continue its work ?

  • @SGaist I'm going to stay away from background operation for the time being. I literally just want to be able to "freeze" when we are switched out, for whatever reason, and resume when we regain foreground context

  • Lifetime Qt Champion

    You should then connect the applicationStateChanged signal and store the state of your application when you get Qt::ApplicationSuspended. Depending on what your application is doing you might want to abort the current operation and restart it when the application resumes.

    If you can do it cleanly you shouldn't need to disconnect any signals.

  • @SGaist what about the socket connection? I won't receive a socket disconnect signal. Should I proactively close it, or just leave it be and check for connection when it switches back to primary focus?

  • Lifetime Qt Champion

    I'd be proactive. That way you always know the state you were in and that might avoid useless bandwidth usage.

  • This post is deleted!

  • @SGaist Thank you!

Log in to reply