Search code examples
javascriptioscordovafileapi

iOS UI freezes during file writing with PhoneGap/Cordova


I'm using the standard PhoneGap plugin for writing to local files via HTML5. In the background my app is downloading data from a server and then saving it to disk as it comes in. Typical file sizes are around 20Mb.

If the user is scrolling the screen or some other UI action when writing is going on, my app will freeze for a second or two on an iPad 2 running iOS7. The file writing operation is wrapped in a setTimeout call (so it doesn't block the main thread) but this doesn't seem to help.

The XCode console reports this while file writing is going on (the lag feels like it happens for about a second or 2 and not the 200ms implied here):

THREAD WARNING: ['File'] took '93.378906' ms. Plugin should use a background thread. THREAD WARNING: ['File'] took '125.793945' ms. Plugin should use a background thread.

What can I do to avoid the lag? How can I diagnose where the lag is happening?


Solution

  • Wrapping up the file writing operations with setTimeout does not prevent the main thread from getting blocked, where did you get that? It just delays the execution of the wrapped function, which in your case doesn't help you at all.

    Nevertheless, I don't even think this is your real problem, because the file plugin's write operation is already wrapped up for execution in a background thread, which tells you the linked line from the iOS plugin source code.

    My guess is there is some other stuff happening, that blocks your UI thread. Which functions from the plugin are you calling exactly? Try to debug/profile the whole process, when the UI lag occurrs, for example in Safari Remote Debugger.

    If that doesn't help you, you should post more specific code which could give some hints about what is happening and what the problem could be.