Set label after an asynchronous method has completed execution

mrterryhmrterryh GBMember
edited April 2015 in Xamarin.iOS

Hi,

I'm building a test app for fun and all is going great so far, but I've bumped into an issue. I have a label in my Storyboard and I can set the text in the viewDidLoad() method. However, I'm also making an HTTP request to retrieve and parse some JSON data using asynchronous methods. In order to follow the single responsibility principle, this is done in a separate class. So in the init method of my view controller, I'm calling the StoreData () method on this class. It looks something like this:

public HomeViewController (IntPtr handle) : base (handle)
{
    pc = new PersonCollection ();
    pc.StoreData ();
}

My question is: how can I set the text on the label AFTER the asynchronous StoreData () method has completed? Can I set a callback of some kind?

Regards

Tagged:

Answers

  • KrystianKrystian USMember
    edited April 2015

    Your class can raise event and controller can subscribe to it.
    `public class PersonCollection
    {
    public event EventHandler OnStoreDataCompleted;

            public void StoreData()
            {
                //your method
                //at the end call event
                if(OnStoreDataCompleted != null)
                    OnStoreDataCompleted(this,yourText);
            }
        }`
    
  • dikogadikoga USMember ✭✭

    Beyond handling the callback, you should call it on main thread.. So, you can use InvokeOnMainThread on your ViewController

  • mrterryhmrterryh GBMember

    @dikoga Thanks for your suggestion. I'm a complete newbie to C# and mobile development – I'm primarily a web developer. Could you provide an example?

  • dikogadikoga USMember ✭✭

    @mrterryh , you know OOP, right?

    So, the same thing you already did, you should inherit...

    https://msdn.microsoft.com/en-US/library/ms173149(v=vs.80).aspx

  • EidandEidand GBMember, University ✭✭✭

    Funnily enough that's exactly one of the things I am doing in one of my articles : http://eidand.com/2015/04/22/xamarin-ios-authentication-using-owin-web-api2-json-web-tokens/

    I've got async calls and when they complete a label is changed. The code is on github and the link is in the article.

    Good luck !

  • NikiNiki BGMember ✭✭✭

    Generally, you should not manipulate views in the constructor. I'd suggest that you move that method in ViewDidLoad.
    Also, instead of using events, you can make StoreData return a task and await that task. Here's some boilerplate to get you started:

    public HomeViewController (IntPtr handle) : base (handle)
    {
        this.pc = new PersonCollection ();
    }
    
    public override async void ViewDidLoad ()
    {
        await this.pc.StoreData ();
        this.MyLabel.Text = "Data stored!";
    }
    

    And in your PersonCollection class:

    public async Task StoreData ()
    {
        /* perform async storing */
    }
    
Sign In or Register to comment.