Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-26030

Android: TCP read() and write() methods cause a crash when running on main UI thread

    Details

    • Story Points:
      7

      Description

      Summary:
      If the "tiapp.xml" file property run-on-main-thread is set to true, then calling a Ti.Networking.Socket.TCP object's blocking read() or write() method will cause a crash on Android.

      Steps to reproduce:

      1. Open the project's "tiapp.xml" file.
      2. Make sure property "run-on-main-thread" is set to true.
      3. Set up an Android device with Internet access.
      4. Build and run TcpClientBlockingTest.js on the Android device.

      Result:
      The app crashes with a Java NetworkOnMainThreadException.

      Cause:
      The Java java.net.ServerSocket class will throw a NetworkOnMainThreadException when used on the main UI thread. It can only be used by a non-UI thread.

      Work-Around:
      Simplest solution is to set the tiapp.xml property "run-on-main-thread" to false.

      Best solution is to not use the blocking read() and write() methods and use the async Ti.Stream.pump() and Ti.Stream.write() methods instead as documented here...
      https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Network.Socket.TCP

      Note that even if we do fix the crash, we still don't recommend using the synchronous read() and write() method calls since they'll block the UI thread. This will make the UI appear to stutter or hang, providing a poor experience for the end-user.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                jquick Joshua Quick
                Reporter:
                jquick Joshua Quick
                Reviewer:
                Christopher Williams
                Tester:
                Lokesh Choudhary
              • Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    TIMOB-6334
                    Sync status:
                    ERROR
                    Last received:
                    Last sent:

                    Git Source Code