Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

iPhone Crashes If Current Window is Closed Too Quickly

I have an iPhone app with a window that fires a custom event that kicks off a DB query, collects data from the result set, iterates over the collected data and creates a view from the data, adding it to the current window. The problem I'm running into is that the app will crash if the window is closed before the looping over the collected data is completed.

I'm logging progress during the loop, so I know the crash isn't occurring during the DB interaction. I've tried adding event listeners to the current window's close and blur events to "short circuit" the loop, but they don't seem to fire in time.

I'm guessing this might be a thread safety problem? Does anyone have any ideas? Thanks.

— asked November 12th 2010 by Matt Grayson
  • 1.4.2
  • 4.1
  • crash
  • iphone
  • mobile
1 Comment
  • I hava the same problem?how do you disable the backbutton on iPhone?I can't find any idea about this~~~

    — commented December 7th 2011 by yu wenbin

3 Answers

  • Accepted Answer

    Without knowing what your code looks like, I'll suggest workarounds that might be possible:

    • listen to the window close event - you can add a listener for this event so you can update a state variable which will indicate that the window has been closed, and avoid trying to add items to it.
    • fetch the db records before opening the window
    — answered November 12th 2010 by Kevin Whinnery
    permalink
    2 Comments
    • I've tried both of those routes with no success. I'll try to get permission to share what I've got, but basically I'm fetching the db records, iterating over them to gather/format the results, closing the db, and then iterating over the collected results to create the child views and add them to the current window. Btw, all of this occurs in the context of an event callback. I'm logging progress throughout the process, so I can see exactly where things fall apart when the app crashes (during the second iteration).

      If I open the window and then close it before it's had time to finish, two things can happen:
      1.) if I wait a few seconds, I can watch the log to see it finish the final iteration and then re-open the window - no crash.
      2.) if I re-open the window immediately without waiting, crash.

      I tried adding listeners to both the blur and close events of the window to halt the loop (and logging when they happen), but from watching the log neither of those events ever gets triggered before the loop has finished - which is why I thought maybe this was a threading issue. Are event callbacks synchronous?

      — commented November 13th 2010 by Matt Grayson
    • I stripped my code down to the bare minimum - no new views or modifying the current window's contents - so that the only thing going on when the window opened was the loop over the pre-fetched DB results. Just an array of objects:

      [{'name': 'Product 1', 'description: 'Yada yada yada ...'}, {'name': 'Product 2', 'description: 'Yada yada yada ...'}]
      

      The window is being opened in the context of a tab group and if the back button is tapped and then the window is re-opened before the first loop has had a chance to finish - crash. For the time time being, I'm just going to disable the back button while the window is loading and then re-enable it once the loop has finished.

      — commented November 15th 2010 by Matt Grayson
  • I guess you would have to find a way to slow down the window close event. Maybe you can set a global flag to tell the DB code to abort the loop.

    Then, have the db loop set another global flag indicating that it's done. Only then would you go ahead with the window close.

    In the window close event you could maybe use the javascript setTimeout() function to keep waiting for a few hundred ms?

    — answered November 12th 2010 by Ingmar Koecher
    permalink
    0 Comments
  • I hava the same problem?how do you disable the backbutton on iPhone?I can't find any idea about this~~~

    — answered December 7th 2011 by yu wenbin
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.