Apache Cordova
  1. Apache Cordova
  2. CB-2190

FileTransfer does not request more time than the 5 second limit when placed in background.

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.2.0
    • Fix Version/s: 2.7.0
    • Component/s: iOS
    • Labels:
      None
    • Environment:

      iOS 6.0.1, Cordova 2.2.0.

      Description

      I've been running into an issue using FileTransfer on iOS that cropped up as part of my upgrade to Cordova 2.2. Previously I had been on PhoneGap 1.2 (a long time between upgrades I know). When using the 1.2 version, I could start a filetransfer upload (using sample code from here: http://docs.phonegap.com/en/2.2.0/cordova_media_capture_capture.md.html#Capture) and it would continue to run if I returned to the home screen or if I locked the device. Now it appears that suspending the app will cause a FileTransferError to occur with error code 3 and error message "The request timed out".

      After trading some emails with Shazron, he pointed out that the limit to finish up background tasks is 5 seconds. If the task will take longer, the app has to request more time – and FileTransfer does not do this currently (although the CDVLocalStorage plugin in onResignActive does: https://github.com/apache/cordova-ios/blob/master/CordovaLib/Classes/CDVLocalStorage.m#L378

      1. CDVFileTransfer.m
        25 kB
        Leif Ullman
      2. CDVFileTransfer.h
        3 kB
        Leif Ullman

        Issue Links

          Activity

          Hide
          Leif Ullman added a comment -

          Proposed fix. Add a UIBackgroundTaskIdentifier as a property of CDVFileTransfer.h. Also kick off upload to background (using beginBackgroundTaskWithExpiration) if using chunkedMode. Finally end the background task in the end of the connectionDidFinishLoading method. This appears to work for me in several test scenarios when kicking off multiple large uploads at once (around 10-20mb each), then sending the application to the background.

          Show
          Leif Ullman added a comment - Proposed fix. Add a UIBackgroundTaskIdentifier as a property of CDVFileTransfer.h. Also kick off upload to background (using beginBackgroundTaskWithExpiration) if using chunkedMode. Finally end the background task in the end of the connectionDidFinishLoading method. This appears to work for me in several test scenarios when kicking off multiple large uploads at once (around 10-20mb each), then sending the application to the background.
          Hide
          Andrew Grieve added a comment -

          I think what you suggests sounds good. If you're willing, please file a pull requests by following the instructions here:

          http://wiki.apache.org/cordova/ContributorWorkflow

          Show
          Andrew Grieve added a comment - I think what you suggests sounds good. If you're willing, please file a pull requests by following the instructions here: http://wiki.apache.org/cordova/ContributorWorkflow
          Hide
          Ian Clelland added a comment -

          I'm merging this into the current (2.5) codebase, and will take a look at how testable it is in the next day or so.

          Show
          Ian Clelland added a comment - I'm merging this into the current (2.5) codebase, and will take a look at how testable it is in the next day or so.
          Hide
          Ian Clelland added a comment -

          Local ad hoc testing suggests that this is working; I'll take any suggestions on how to automatically test this within the device.

          Pull request here: https://github.com/apache/cordova-ios/pull/40

          Show
          Ian Clelland added a comment - Local ad hoc testing suggests that this is working; I'll take any suggestions on how to automatically test this within the device. Pull request here: https://github.com/apache/cordova-ios/pull/40
          Show
          Andrew Grieve added a comment - Fix commit: https://git-wip-us.apache.org/repos/asf?p=cordova-ios.git;a=commit;h=7aa2607416c4a3981e7274e825457bacdcbc040b
          Hide
          Jan Pittner added a comment -

          This issue is only fixed for (chunked) uploads, not downloads. Download tasks are aborted and do not request time. Also, downloads don't run in a background thread, which substantially slows down the UI. Any chance this can get re-opened and fixed for downloads?

          Show
          Jan Pittner added a comment - This issue is only fixed for (chunked) uploads, not downloads. Download tasks are aborted and do not request time. Also, downloads don't run in a background thread, which substantially slows down the UI. Any chance this can get re-opened and fixed for downloads?
          Hide
          Jan Pittner added a comment -

          To continue downloads once the app is put in background, it's as simple as copying the backgroundTaskId code that's used in the upload function and putting it in the download function. Each file transfer delegate gets its own backgroundTaskId, and as the download finishes, the backgroundtask is removed for that download. But the downloads themselves are still on the main thread, and 2 concurrent downloads of 100mb+ files on an iPhone 5 are enough to slow the UI noticeably.

          I'm not sure why this bug was closed and marked as fixed when clearly only 1/2 of it was addressed. I'm going to try and figure out background downloads by looking at how ASIHttpRequest does it. Once I figure that out hopefully I'll be able to submit a patch.

          Show
          Jan Pittner added a comment - To continue downloads once the app is put in background, it's as simple as copying the backgroundTaskId code that's used in the upload function and putting it in the download function. Each file transfer delegate gets its own backgroundTaskId, and as the download finishes, the backgroundtask is removed for that download. But the downloads themselves are still on the main thread, and 2 concurrent downloads of 100mb+ files on an iPhone 5 are enough to slow the UI noticeably. I'm not sure why this bug was closed and marked as fixed when clearly only 1/2 of it was addressed. I'm going to try and figure out background downloads by looking at how ASIHttpRequest does it. Once I figure that out hopefully I'll be able to submit a patch.
          Hide
          Andrew Grieve added a comment -

          Thanks Jan Pittner. I've re-opened the issue.

          Patch would be great! If you do submit one, please know that you'll need to sign Apache's Contributor License Agreement before we can merge it in (can be done online).

          http://www.apache.org/licenses/#clas

          Show
          Andrew Grieve added a comment - Thanks Jan Pittner . I've re-opened the issue. Patch would be great! If you do submit one, please know that you'll need to sign Apache's Contributor License Agreement before we can merge it in (can be done online). http://www.apache.org/licenses/#clas
          Hide
          ASF subversion and git services added a comment -

          Commit a588236cbdb3c81e2b38b8bfc036aa89520b28d5 in cordova-plugin-file-transfer's branch refs/heads/dev from Jan Pittner
          [ https://git-wip-us.apache.org/repos/asf?p=cordova-plugin-file-transfer.git;h=a588236 ]

          CB-2190 Make backgroundTaskId apply to downloads as well. Move backgroundTaskId to the delegate.

          Show
          ASF subversion and git services added a comment - Commit a588236cbdb3c81e2b38b8bfc036aa89520b28d5 in cordova-plugin-file-transfer's branch refs/heads/dev from Jan Pittner [ https://git-wip-us.apache.org/repos/asf?p=cordova-plugin-file-transfer.git;h=a588236 ] CB-2190 Make backgroundTaskId apply to downloads as well. Move backgroundTaskId to the delegate.
          Hide
          Andrew Grieve added a comment -

          Fixed in 0.4.2-dev

          Show
          Andrew Grieve added a comment - Fixed in 0.4.2-dev

            People

            • Assignee:
              Andrew Grieve
              Reporter:
              Leif Ullman
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development