Last active
May 12, 2018 19:20
-
-
Save shanmugasanthosh7/9509504b1a3e5ee6960ab5e2a234cf5d to your computer and use it in GitHub Desktop.
Combine two API call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity : AppCompatActivity | |
{ | |
private Post post; | |
private ToDo toDo; | |
protected override async void OnCreate(Bundle savedInstanceState) | |
{ | |
base.OnCreate(savedInstanceState); | |
SetContentView(Resource.Layout.activity_main) | |
var textview = FindViewById<TextView>(Resource.Id.textView); | |
var apiService = NetworkService.GetApiService(); | |
var getTodo = apiService.GetToDo().ContinueWith(toDo => this.toDo = toDo); | |
var getPost = apiService.GetPost().ContinueWith(post => this.post = post); | |
await Task.WhenAll(getToDo,getPost) // Wait for complete the two tasks. | |
.ContinueWith(result => | |
{ | |
if (result.IsCompleted && result.Status == TaskStatus.RanToCompletion) | |
{ | |
// Get result and update any UI here. | |
textView.Text = post.Title; // For property serialized/deserialized using Newtonsoft.Json | |
var toDoTitle = toDo.Title; | |
} | |
else if (result.IsFaulted) | |
{ | |
// If any error occurred exception throws. | |
} | |
else if (result.IsCanceled) | |
{ | |
// Task cancelled | |
} | |
},TaskScheduler.FromCurrentSynchronizationContext())// execute in main/UI thread. | |
.ConfigureAwait(false)// Execute API call on background or worker thread. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment