Author Topic: Issue: UIInput on iPhone fails to edit 5% to 20% of the time  (Read 3552 times)

niniane

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 7
    • View Profile
I am using an UIInput on an iPhone application.  At least 5% of the time, when the keyboard comes up, my edits fail to propagate to the label.  Repro steps:

1. I created a sprite with UIInput, with a child Label.
2. I ran this on iPhone.  I press the sprite to bring up the mobile keyboard.
3. I change the text from "A" to "B".  I see that the label changed to "B".
4. I press "Done".
5. Repeat steps 3 and 4, changing "B" to "C", press Done, then "C" to "D", press Done, etc. 

Within 5 to 20 iterations, I will hit a case where the label won't change, no matter what I type on the keyboard.  If the label says "N", it will say "N" no matter what I type in the keyboard.  If I type "O", the keyboard preview would say "O", but the label still says "N".  After I hit "Done", the label still reads "N" as though I had not edited it.

I have never gotten through the alphabet without encountering the bug at least once. 

I created an entirely new test project with just the UIInput sprite, and this still happens. 

I am using NGUI 3.6.5, but this also happened with 3.5.9.  Would you please look into what's causing this?  We have been trying to debug it ourselves for days, and it's blocking us from giving our mobile application to testers.  Thank you.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Issue: UIInput on iPhone fails to edit 5% to 20% of the time
« Reply #1 on: June 24, 2014, 04:52:22 AM »
What are your UIInput's settings? Just defaults? No validation, no hidden input? Anything else abnormal happening while the bug occurs? Is it only on iOS, not reproduceable anywhere else?

If the input isn't hidden, the mobile keyboard handling hasn't changed in years. You can see what happens around line 600 of UIInput.cs. NGUI retrieves the text from the touch screen keyboard, and if the value is different from the cached value, the input is updated. Can you add some NGUIDebug.Log statements there to see which parts of the code it's hitting when the issue occurs and what mKeyboard.text is, as well as what mCached is?

niniane

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 7
    • View Profile
Re: Issue: UIInput on iPhone fails to edit 5% to 20% of the time
« Reply #2 on: June 24, 2014, 12:23:38 PM »
I've been debugging this, and it may be a race condition in UIInput.Update().  Around line 589, the code creates a new keyboard:

          mKeyboard = (inputType == InputType.Password) ?
            TouchScreenKeyboard.Open(val, kt, false, false, true) :
            TouchScreenKeyboard.Open(val, kt, !inputShouldBeHidden && inputType == InputType.AutoCorrect,
              label.multiLine && !hideInput, false, false, defaultText);

But when it gets to line ~633, mKeyboard.active is still false (because the keyboard didn't have time to fully initialize yet).

        if (mKeyboard.done || !mKeyboard.active)
        {
          if (!mKeyboard.wasCanceled) Submit();
          mKeyboard = null;
          isSelected = false;
          mCached = "";
        }

So this code sets mKeyboard = null.  The iPhone still opens the keyboard, but by the time it's open, it's unattached to the mKeyboard variable in UIInput.  So all of my keypresses get ignored because mKeyboard has been set to null, and isSelected has been set to false.

I attached a screenshot of my UIInput settings.  No validation, no hidden inputs.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Issue: UIInput on iPhone fails to edit 5% to 20% of the time
« Reply #3 on: June 25, 2014, 05:55:13 AM »
@niniane: This was already resolved a while back. You should update. NGUI exits the function after opening the keyboard now.

niniane

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 7
    • View Profile
Re: Issue: UIInput on iPhone fails to edit 5% to 20% of the time
« Reply #4 on: June 25, 2014, 01:32:42 PM »
No, even though NGUI exits Update() after opening the keyboard, the problem happens on the next iteration through Update(). 

On the second iteration through Update(), mKeyboard.active is still false, so the code I quoted above (~line 633) sets mKeyboard to null.

I put in a fix by adding a boolean variable "mKeyboardStarting" and adding another check to line 633:
        if (!mKeyboardStarting && (mKeyboard.done || !mKeyboard.active))

You also then need to add:
        if (mKeyboardStarting && mKeyboard.active) {
          mKeyboardStarting = false;
        }

I verified that this fixes the problem.

I just updated yesterday to NGUI v3.6.5.  This bug was reproducible in v3.6.5 until I put in the fix I wrote above. 

Would you incorporate this fix so that we can upgrade to future versions of NGUI without having to keep putting in this fix? Thank you.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Issue: UIInput on iPhone fails to edit 5% to 20% of the time
« Reply #5 on: June 26, 2014, 10:26:12 AM »
Thanks. I made similar changes. I added the following to the Update() function right after "if (mDoInit) Init();" part:
  1. #if MOBILE
  2.                         // Wait for the keyboard to open. Apparently mKeyboard.active will return 'false' for a while in some cases.
  3.                         if (mWaitForKeyboard)
  4.                         {
  5.                                 if (!mKeyboard.active) return;
  6.                                 mWaitForKeyboard = false;
  7.                         }
  8. #endif
And I added "mWaitForKeyboard = true;" right above where I open the keyboard. mWaitForKeyboard is a static boolean member. Does this address the issue?

niniane

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 7
    • View Profile
Re: Issue: UIInput on iPhone fails to edit 5% to 20% of the time
« Reply #6 on: July 17, 2014, 07:54:45 PM »
ArenMook, I tested the fix you quoted (with "mWaitForKeyboard"), and that works for me also.  Thank you for including this fix in future versions of NGUI. 

Keep up the great updates!