Bug 56536 - NSUrlSessionDownloadDelegate Methods Not Called After Resuming Application
Summary: NSUrlSessionDownloadDelegate Methods Not Called After Resuming Application
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 10.8 (d15-1)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2017-05-16 23:49 UTC by adam
Modified: 2017-05-17 16:13 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description adam 2017-05-16 23:49:24 UTC
Some NSUrlSessionDownloadDelegate methods stop being called after the user backgrounds the app and then reopens the application on iOS 10.3.1 on physical iPhone devices (this is where I have been able to repeatedly reproduce the problem but it may exist on other devices as well).  

The DidWriteData only gets called prior to the user backgrounding the application (and is never called after the user re-opens the app), and all of the remaining DidFinishDownloading overrides get bunched up and all sent through in one giant burst at the end of the download prior to the DidFinishEventsForBackgroundSession call.  So we are able to detect a completed background transfer service but we do not get any progress or files finishing after re-opening the application which makes it impossible to show progress.

Steps to reproduce:
1) Download the following sample Xamarin.Forms project: http://arteksoftware.com/backgrounding-with-xamarin-forms/

2) In the DownloadPage.xaml.cs class change the downloadButton click handler's Url property to a larger remote file (such as a large zip on your own server - I am testing a ~150 MB zip file in Azure Storage)

3) In the DownloadPage.xaml.cs class also comment out the following line (since we are no longer downloading the image provided in the sample): //commented out: catImage.Source = FileImageSource.FromFile(message.FilePath);

4) After commenting out the line above let's make a different change on the UI thread so we can tell when the download finishes: downloadButton.Text = "FINISHED!!!";

5) Next we'll run this iOS project on a few different devices (with varying results of success).

6) Build and run this solution on an iPhone running iOS 9 (I'm running this on an iPhone 6 running iOS 9.3.5).  Watch the 
progress text increment, press the home button to send the app to the background, wait 5 - 10 seconds, re-open the app and notice the progress text catches up and continues to increment as expected.

7) Next build and run this solution on an iOS 10 phone device (I'm running this on an iPhone 7 Plus running iOS 10.3.1).  Watch the progress text increment, press the home button to send the app to the background, wait 5 - 10 seconds, re-open the app and notice the progress text never catches up and does not ever increment.  This actually does occasionally work for me (the progress resumes and increments as expected but more often than not it never does).

8) Next build and run this solution on an iOS 10 tablet device (I'm running this on a brand new iPad 5th generation running iOS 10.3).  Watch the progress text increment, press the home button to send the app to the background, wait 5 - 10 seconds, re-open the app and notice the progress text catches up and continues to increment as expected.

The major issue I'm facing here would obviously be with step number 7 above (iPhone 7 Plus running iOS 10.3.1).  Again if it worked successfully for you please re-run and test again, for me the phone's progress text fails to increment after re-opening the application 8 or 9 times out of 10.

I've uploaded a screen recording of myself testing the code on the 3 devices I mentioned above along with their results:

Here is my Xamarin Studio Community Details:
=== Xamarin Studio Community ===

Version 6.3 (build 863)
Installation UUID: 0488e1aa-8dac-4637-8838-c48b36b83b21
	Mono 4.8.1 (mono-4.8.0-branch/22a39d7) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408010000

=== NuGet ===


=== Xamarin.Profiler ===

Version: 0.32.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 8.3.2 (12175)
Build 8E2002

=== Xamarin.Mac ===

Xamarin.Mac not installed. Can't find /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/Version.

=== Xamarin.iOS ===

Version: (Xamarin Studio Community)
Hash: a04678c2
Branch: d15-1
Build date: 2017-03-28 14:05:38-0400

=== Xamarin.Android ===

Version: (Xamarin Studio Community)
Android SDK: /Users/azcreatescode/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3  (API level 15)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
		6.0    (API level 23)
		7.0    (API level 24)

SDK Tools Version: 25.2.2
SDK Platform Tools Version: 25
SDK Build Tools Version: 25

Java SDK: /usr
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

Android Designer EPL code available here:

=== Xamarin Inspector ===

Version: 1.2.2
Hash: b71b035
Branch: d15-1
Build date: Fri, 21 Apr 2017 17:57:12 GMT

=== Build Information ===

Release ID: 603000863
Git revision: a2163670efe259c85cd8f335d95b175068fbbe2a
Build date: 2017-04-03 14:33:15-04
Xamarin addins: 2045d688ea1420e0381b473360ca62a763eb7d04
Build lane: monodevelop-lion-d15-1

=== Operating System ===

Mac OS X 10.12.4
Darwin AZCreatesCodes-MacBook-Pro.local 16.5.0 Darwin Kernel Version 16.5.0
    Fri Mar  3 16:52:33 PST 2017
    root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

Redth's Addins 1.0.7

Please let me know if you have any questions.

Comment 1 adam 2017-05-17 15:10:15 UTC
I have commented on the issue here on the Apple Developer Forum:

I have also submitted a bug to Apple:

The issue Problem ID # is: 32247561
Comment 2 Vincent Dondain [MSFT] 2017-05-17 16:13:07 UTC
Hi Adam,

If I follow correctly your, very detailed, bug report and the various comments on Apple's forums.

My understanding of the situation is that this is clearly a native iOS bug. You confirmed it in your forum comments by testing with a native sample using Xcode and filing a radar was the right thing to do in this case.

Xamarin.iOS is a very thin layer on top of the native iOS frameworks so there's nothing else we can do here, therefore I will close this bug.

It's still there for documentation purposes and feel free to share any update on the radar (that only you can see) here.