Forum Xamarin.iOS

Opening new viewcontroller from non-main thread

tactyytactyy Member ✭✭

i've been at this for hours and for the life of me cant get it to open a new view, getting frustrating so maybe somebody can help, im trying to open a new view after a user logs in (Receives response from network on a separate thread)

The code runs and all debugs are triggered but the new viewcontroller never seems to open, any help is appreciated,
thankyou in advance

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Will the Trig#3 be printed out?
    If view could be generated successfully and this.NavigationController is not null, this should work.
    Try to check it with a simple navigating:

    Task.Run(() =>
    {
        InvokeOnMainThread(() =>
        {
            // Perform navigating
        });
    });
    
  • tactyytactyy Member ✭✭

    Well thats why im confused, all the trig's are printed out but my screen never changes, if i remove all the networking and just call the same code from the main thread it works fine so dont think its an issue with the nav controller or anything, got me stuck :/

  • LandLuLandLu Member, Xamarin Team Xamurai

    You could share a sample here. Use

    Task.Run( async () =>
    {
        await Task.Delay(2000);
    });
    

    To simulate the network thread.

  • tactyytactyy Member ✭✭

    Im still new to threading and tasks so not sure if this is correct but i tried

                    Task.Run(async () => {
                        await Task.Delay(2000);
    
                        vc_ParticipantRegister view = this.Storyboard.InstantiateViewController("ParticipantRegister") as vc_ParticipantRegister;
                        this.NavigationController.PushViewController(view, true);
                    });
    

    i put this in my ViewDidLoad method and waited for more then 10 seconds and it never opened a new view, so unsure what that means

  • tactyytactyy Member ✭✭

    After alot more testing it seems that InvokeOnMainThread() just does not work in my case, im calling InvokeOnMainThread() from a Timer() thread, i dont know if anybody else has had similar problems but its really frustrating because i have an entire storyboard setup and cannot open views, :/ i think ive spent more hours on this 'little' problem than i have on the entire android equivalent of this application, so any help is very appreciated!...

  • LandLuLandLu Member, Xamarin Team Xamurai

    What I mean is:
    Task.Run makes the code inside runs on the secondary thread. And then Task.Delay simulates an internet request. Finally, use InvokeOnMainThread to return to the main thread.
    Here is the entire code of a button click event:

    partial void BtnClick(UIKit.UIButton sender)
    {
        Task.Run( async () =>
        {
            await Task.Delay(2000);
            InvokeOnMainThread(() =>
            {
                ViewController vc = Storyboard.InstantiateViewController("ViewController") as ViewController;
                NavigationController.PushViewController(vc, true);
            });
    
        });
    }
    

    And it pushes to a new vc successfully:

    If you really can't make it work. Share your sample here.

  • tactyytactyy Member ✭✭

    so it works perfectly when called from ViewDidLoad()

    public override ViewDidLoad() {
        base.ViewDidLoad();
    
        Task.Run(() => {
            vc_ParticipantRegister vc = this.Storyboard.InstantiateViewController("ParticipantRegister") as vc_ParticipantRegister;
            this.NavigationController.PushViewController(vc, true); 
        });
    }
    

    but still dosn't work when i call it from my NetworkManager.cs class
    im using a Timer.Elapsed() method to receive network data, i wonder if that is affecting it in any way, would be wierd but all i can think of

    I Appreciate The Help Btw

  • LandLuLandLu Member, Xamarin Team Xamurai

    We need your sample to reproduce the issue you described.
    I don't know what happened on your side without it.

Sign In or Register to comment.