Addev
Addev

Reputation: 32253

Should I convert the async calls to sync in Windows Phone 8?

I'm a pretty experienced programmer that used to program in Android and recently started programming for Windows Phone. When I discovered the async/await paradigm introduced in C# 5.0.

I liked but programming my first app noticed that a simple network connection for a login request in example, makes the async to spread all along the code, from the network layer to the presentation. It is shocking for me me because it forces me to rewrite all the interfaces, making them return Tasks instead of the original result type.

In example

public LoginRespose login(string username, string password)

gets converted into

public Task<LoginResponse> login(string username,string password);

I have seen a lot of helper classes that allows to convert an async call into synchronous. Should I use one of them to "cut" the async chain in the network layer and take care of calling it asynchronously from the UI in order to avoid blocking the main thread?

Upvotes: 1

Views: 103

Answers (2)

Cologler
Cologler

Reputation: 714

like:

var task1 = ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName, 
    CreationCollisionOption.OpenIfExists).AsTask();
task1.Wait();
var logFolder = task1.Result;

Upvotes: 0

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149598

Should I use one of them to "cut" the async chain in the network layer and take care of calling it asynchronously from the UI in order to avoid blocking the main thread?

Don't. async is ment to propagate all the way up the call chain. That is why the pattern says "async all the way".

I see why you would be "shocked" at first. It is a bit overwhelming to understand that your entire call hierarchy needs to be asynchronous, I.E adding additional Task or Task<T> methods in order to use async. But let it grow on you, it is definitely the way to go. Don't block on async code, embrace it instead.

For example, use IAsyncCommand instead of ICommand which handles asynchronous operations.

Upvotes: 5

Related Questions