Bug 21694 - Using NSUrlSession.CreateDownloadTask() with completion handler or async crashes
Summary: Using NSUrlSession.CreateDownloadTask() with completion handler or async crashes
Status: RESOLVED INVALID
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 7.2.5
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-07-30 10:01 UTC by René Ruppert
Modified: 2014-07-30 16:24 UTC (History)
4 users (show)

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


Attachments
Xcode Project replicating issue (30.67 KB, application/zip)
2014-07-30 16:24 UTC, Alex Soto [MSFT]
Details


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:
Status:
RESOLVED INVALID

Description René Ruppert 2014-07-30 10:01:43 UTC
The following two code snippets try to create a NSUrlSessionDownloadTask. Both fail with the same exception (see below).

NSUrlSession session = ...;

Synchronous with completion handler:
this.session.CreateDownloadTask (NSUrl.FromString (downloadInfo.Source), (NSData data, NSUrlResponse response, NSError error) =>
{
Console.WriteLine ("Task created. Response: {0}, Error: {1}", response, error);
});

Async: 

var completionHandler = await this.session.CreateDownloadTaskAsync(NSUrl.FromString (downloadInfo.Source), out downloadTask);


The overloads without completion handler work fine, however as creation of a download task can fail, the completion handler is kind of mandatory.

Exception:

MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSGenericException Reason: Completion handler blocks are not supported in background sessions. Use a delegate instead.
  at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSend_IntPtr_IntPtr (intptr,intptr,intptr,intptr)
  at MonoTouch.Foundation.NSUrlSession.CreateDownloadTask (MonoTouch.Foundation.NSUrl url, MonoTouch.Foundation.NSUrlSessionResponse completionHandler) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/build/compat/Foundation/NSUrlSession.g.cs:376
  at FileTransfer.FileTransferController+<HandleActionRequested>c__async0.MoveNext () [0x0008f] in /Users/rene/Documents/Dropbox/Firma/Xamarin/Evolve/Talks/iOSBackgrounding/Demo/FileTransfer/FileTransferController.cs:91
  at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[<HandleActionRequested>c__async0] (FileTransfer.<HandleActionRequested>c__async0& stateMachine) [0x0001b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/AsyncVoidMethodBuilder.cs:106
  at FileTransfer.FileTransferController.HandleActionRequested (FileTransfer.DownloaderCell cell, MonoTouch.UIKit.UIButton button, TRANSFER_ACTION action) [0x00000] in <filename unknown>:0
  at FileTransfer.DownloaderCell.HandleStartDownload (MonoTouch.UIKit.UIButton sender) [0x00009] in /Users/rene/Documents/Dropbox/Firma/Xamarin/Evolve/Talks/iOSBackgrounding/Demo/FileTransfer/DownloaderCell.cs:75
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at FileTransfer.Application.Main (System.String[] args) [0x00008] in /Users/rene/Documents/Dropbox/Firma/Xamarin/Evolve/Talks/iOSBackgrounding/Demo/FileTransfer/Main.cs:17


=== Xamarin Studio ===

Version 5.1.4 (build 0)
Installation UUID: 220dde4a-6e9c-4c80-a89e-6ef72bea0759
Runtime:
	Mono 3.4.0 ((no/954ed3c)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000214

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.Mac ===

Version:

=== Xamarin.Android ===

Version: 4.12.6 (Business Edition)
Android SDK: /Users/rene/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.2   (API level 8)
		2.3   (API level 10)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Xamarin.iOS ===

Version: 7.2.5.5 (Business Edition)
Hash: 87026b6
Branch: 
Build date: 2014-07-02 19:39:08-0400

=== Build Information ===

Release ID: 501040000
Git revision: 7d45bbe2ee22625f125d0c52548524f02d005cca
Build date: 2014-07-14 12:15:53-04
Xamarin addins: c78f1d88e57baa928aeee1484d96e6f8edf8de33

=== Operating System ===

Mac OS X 10.9.4
Darwin iMacBuero.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 1 Sebastien Pouliot 2014-07-30 10:21:22 UTC
This is a native (from iOS) exception. It tells you:

> Completion handler blocks are not supported in
> background sessions. Use a delegate instead.

which makes me assume, from that message, that it will only work when the application is active (foreground) not on the background.

Is your test case running on the background ? (the path of the files suggests it is)
Comment 2 René Ruppert 2014-07-30 10:24:08 UTC
No, the app is in the foreground. The code above gets executed in the click handler of a button.
Comment 3 Alex Soto [MSFT] 2014-07-30 16:23:51 UTC
Hello 

This exception is expected and can be reproduced in an ObjC app too (see attached xcode Project)

"Completion handler blocks are not supported in background sessions. Use a delegate instead."

downloadTaskWithURL:completionHandler: is not meant to be used in combination of BackgroundSessionConfiguration. You should use downloadTaskWithURL: and use the delegate methods or you must use other SessionConfiguration in order to use downloadTaskWithURL:completionHandler:

more info on apple docs [1] under de the section of Background Transfer Considerations 

Hope this helps

Alex

[1]: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html
Comment 4 Alex Soto [MSFT] 2014-07-30 16:24:33 UTC
Created attachment 7537 [details]
Xcode Project replicating issue