Bug 28425 - Can not await NSUrlSession.CreateDataTaskAsync in some scenarios
Summary: Can not await NSUrlSession.CreateDataTaskAsync in some scenarios
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Rolf Bjarne Kvinge [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2015-03-25 15:43 UTC by Evgeny
Modified: 2015-03-31 13:35 UTC (History)
4 users (show)

Tags:
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:
Status:
RESOLVED FIXED

Description Evgeny 2015-03-25 15:43:16 UTC
Continuation for the await session.CreateDataTaskAsync(request) is never called in the code below ("Download complete" message is never printed):


NSUrlSession session = NSUrlSession.SharedSession;
var dataRequest = await session.CreateDataTaskAsync(new NSUrlRequest(new NSUrl("http://www.xamarin.com")));;

Logger.Debug("Download completed");



This code has the same problem:

NSUrlSessionDataTask downloadTask = null;
NSUrlSession session = NSUrlSession.SharedSession;

var dataRequest = await session.CreateDataTaskAsync(new NSUrlRequest(new NSUrl("http://www.xamarin.com")), out downloadTask);

Logger.Debug("Download completed");



Where as the following code works fine:

NSUrlSessionDataTask downloadTask = null;
NSUrlSession session = NSUrlSession.SharedSession;
var task = session.CreateDataTaskAsync(new NSUrlRequest(new NSUrl("http://www.xamarin.com")), out downloadTask);
downloadTask.Resume();

var dataRequest = await task;

Logger.Debug("Download completed");


It seems like the first snippet of code should work (the task should be resumed automatically).
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-03-26 05:36:18 UTC
@Marek, can you comment on this whether it's a bug or an issue with expectations?
Comment 2 Marek Safar 2015-03-26 19:09:02 UTC
Not really await/async issue but some block magic issue, same problem occurs without async

			NSUrlSession session = NSUrlSession.SharedSession;
			var mm = new NSUrlRequest(new NSUrl("http://www.xamarin.com"));

			var res = session.CreateDataTask(mm, (data, response, error) => {
				return; // THIS line is never hit
			});
Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-03-31 13:35:52 UTC
OK, I see the problem.

It looks like we bound the Objective-C API without looking too much at how it's supposed to work.

I've fixed the API that don't have the 'out task' parameter to automatically call resume (since those API are currently useless). I have not touched the API with the 'out task' parameter, since that could break existing apps.

maccore/master: c8dd4a203e94e45ef06833a54d32f3e93b7acd6f

QA: unit tests were added.