PushAsync() and delay

NayZaKNayZaK USMember ✭✭
edited August 2014 in Xamarin.Forms

I'm wondering how to avoid delation after Navigation.PushAsync() was called. Where do I need to post all my initialization code for view and view model? At a moment my init code is in constructors of view and view model classes and my app has a delay of about half a second before push animation starts.
P.S. Using iOS

Posts

  • CraigDunnCraigDunn USXamarin Team Xamurai

    What is your initialization code doing that you think it's causing a delay? Can you move any long-running tasks onto another thread? Obviously it will still take the same length of time, but you can show an ActivityIndicator or something on the new screen until the loading work is complete.

  • NayZaKNayZaK USMember ✭✭
    edited August 2014

    What is your initialization code doing that you think it's causing a delay?

    Nothing criminal. View calls init method to parse XAML, view model initialize binding properties with constant data. No database calls, no network activity.

    Maybe Xamarin Labs MVVM helpers causes a problem?

  • CraigDunnCraigDunn USXamarin Team Xamurai

    Can't really comment on that - if you can, start commenting things out as a basic way of seeing what's causing the delay. Like I said, the way around it is to move the long-running tasks onto another thread.

  • NayZaKNayZaK USMember ✭✭

    I'll try to figure out the reason of delays and will post my results in couple of days.

    Craig Dunn, thanks for reply.

  • NayZaKNayZaK USMember ✭✭
    edited August 2014

    I build on iPod Touch 2 apps: XamlSamples and a very simple mvvm app using Xamarin Forms Labs. As a result XamlSamples make push animation immediately after users action, but the second app performs with a little delay. But there is now delation on iOS Simulator. I think, that conclusion is obvious...

  • NayZaKNayZaK USMember ✭✭
    edited August 2014

    And me again. I removed XForms Labs from my project, but still notice delays.

    First of all problem is in XAML. If xaml file is a little bit "complex" (in my case it's just about 80 lines of code with scroll view, stack layouts, labels, buttons and images), than it takes some time to parse it. For example, if I'm not binding any data to view and my ContentPage without any content, then page pushes immediately. But when I put content (see above), than I notice a small delay. So why xaml files are parsing on fly? Why they are not compiled?

    Secondly, why bindings are so slow? I played around my problem with delays and found out that bindings are harmful for performance. Example. If I set up binding context with instantiated constant data (strings for labels and images urls) before InitializeComponent() is called than delay is very noticeable and worsens user experience. If I instantiate bindable data after instantiating of view components than page pushes with expected speed (don't forget about delays on "complex" xamls) but after page pushed components are rendered with default data and after about a 50-100ms components takes they binding data. And I don't know what is more ugly: forcing user to wait before new page will appear with rendered data or show him blank page which will render data without any animation.

    And again, all my tests are on iPod Touch 5gen. On iOS Simulator everything fine.

    Any comments or suggestions? I can make a video for illustration.

  • NayZaKNayZaK USMember ✭✭

    1 - Setting BindingContext with instantiated data after InitializeComponent() and before PushAsync()

    2 - Setting BindingContext with instantiated data before InitializeComponent() and before PushAsync()

    3 - Setting BindingContext with instantiated data after InitializeComponent() and after PushAsync()

    4 - BindingContext not set

    5 - Setting BindingContext with instantiated data after InitializeComponent() and before PushAsync(). No XAML. Just Label in ScrollView. Labels TextProperty is binded.

  • NayZaKNayZaK USMember ✭✭

    And nobody had same issues? What I'm doing wrong?

  • xcodexcode USMember

    me too ,
    i found that if the page is containing a complex layout , when pushasync this page .
    it will some delay on this.

    i think it is xamarin problem.

  • NayZaKNayZaK USMember ✭✭

    I think problem is in layouts as was mentioned in this forum. For example list with ViewCells is very laggy... worse then on android devices :)

  • rmarinhormarinho PTMember, Insider, Beta Xamurai
    edited August 2014

    Bindings will always slow you down a little bit.. This is true even outside Xamarin Forms, (Windows Phone, Silverlight, wpf)

    I have found that sometimes is better to just forget binding and normally set the values.. (for example the 1st page of your app, u don't want it to be slow)

    I don0t think Labs is the reason, but i could be wrong.

    also make sure you don't have too much code on the ctor, expose a method called Init on your vm, and on appearing call that method that setups the data u need on your vm.

  • ElskurkosElskurkos USMember

    Any progress here? I'm having similar issues as well.

    At the moment, before i PushAsync I instantiate both my ViewModel and my View. While the view is displayed fairly fast it takes ~250-800 ms (depending on view) before I can interact with buttons etc.

  • CliffCawleySkeduloCliffCawleySkedulo USMember ✭✭

    I've also found like @NayZaK that Bindings are slow and staggered with a delay between each.

    As a test if I have 100 controls on the screen, all bound to different properties and then in code I iterate and update each one (with no delays, just a for loop), I will see the view update one by one with what looks like a 10-50ms delay between each next control getting updated from the updated binding. So instead of them all updating in one go, you see the values update over time and it looks really bad.

    I don't suppose someone at Xamarin left a Delay or Sleep in by accident in the databinding code?!

  • NickKovalskyNickKovalsky USMember ✭✭✭

    Forms navigation shows up to be very slow indeed, especially when you want to have tabbed pages and on android..
    One of the workarounds i used to avoid freeze between the user clicks and pushed content shows up is turning pushing animation off and using Activator.

            var page = (Page)Activator.CreateInstance(typeof(MyPageClass)); //note you can specify constructor params here if needed
        await NavigationService.PushAsync(Navigation, page, false); //the false speeds up things tremendously
    

    The other workaround is awful but i was still using it: pushing an empty transition page with logo and loading text that would show instantly after click to avoid freezes, and was loading the next real page at the same time. That transition page would pop async itself so the for the user it would be just a loading screen shown for a short moment, to smooth the input reaction..

    If i did no workarounds then users were clicking several times on same button cause with the frozen screen you cant even see the button is disabled.. I hope next Forms version will rock-rocket.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @NickKovalsky

    If i did no workarounds then users were clicking several times on same button cause

    My workaround was to make a popup (rg.plugins.popups) that was nothing more than a 1% white: Just enough to be a valid hit-test but not enough to obscure my page. Nothing else: Just a full screen pop. That is my BlockerPopup. It exists for the lifecycle of the app and therefore pops really fast.

    During a page navigation I push the blocker so the user can't hit anything, then push the Page underneath the popup, then remove the popup. It doesn't make anything faster but it does avoid all those horrible multi-taps on everthing.

    But I must try adding in your .CreateInstance to see if it speeds the first time appearance of a Page noticeably.

  • CaioshinCaioshin ITMember ✭✭

    Good suggestion, I tried to remove animation from my transition, measuring the time passed between the tap event on a button and the page appearing event doesn't change performance.

    I also tried to create an instance of the new page before navigate to that page, but this doesn't affect performance because using Freshmvvm a new instance of page (and pagemodels) are created every time.

  • RaelsonCraftzRaelsonCraftz BRMember ✭✭
    edited May 2018

    Hi there!

    I was suffering from a similar issue. After I turned off the navigation bar everywhere (i like to use my own navigation bar in a grid on the top, instead of the masterdetail page). Suddenly, the pages started to push instantly. I'm turning it off like this:

    NavigationPage.SetHasNavigationBar(this, false);

    This at the constructor of each content page in my project. Apparently it helped (a lot), hope it can a solution for some people. The navigation still works normally, it's just the bar that is deactivated.

    EDIT.: apparently it was happening in my case when the navigation bar was turned off only in the first page, but not on the other ones

Sign In or Register to comment.