Forum Xamarin.iOS

WillEnterForeground - Refresh View

rrodriguezrrodriguez USMember
edited October 2013 in Xamarin.iOS

I have an app that list items from the server.
It has a local storage for those items.

The idea is that the app will always shows, at least, the items in the local cache, while refreshing from the server every time this particular controller/view is presented.

So, on the ViewWillAppear I'm synchronously loading the items from my local storage
AND on ViewDidLoad I'm asynchronously requesting the items from the server, if I get new items, the view is refreshed.

This works fine.
The problem comes when I press home in the iphone and my app is sent to background, when it comes back to the foreground I was expecting ViewDidLoad to execute, but that is not the case.

Is there any handler on the view I can use to pull the items from the server?
Is WillEnterForeground my only option?

How would you do this?

Thanks,
R.

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    WillEnterForeground is the right way to do that, but you can use NSNotificationCenter to do it instead of modifying your app delegate code:

    private NSObject _notificationHandle;
    
    public override void ViewWillAppear(bool animated)
    {
        _notificationHandle = NSNotifcationCenter.DefaultCenter.AddObserver(UIApplication.WillEnterForegroundNotification, HandleAppWillEnterForeground);
    }
    
    public override void ViewDidDisappear(bool animated)
    {
        NSNotifcationCenter.DefaultCenter.RemoveObserver(_notificationHandle);
    }
    
    private void HandleAppWillEnterForeground(NSNotification notification)
    {
        // Do your work here (but don't block!)
    }
    
  • rrodriguezrrodriguez USMember

    Awesome! I was hoping there was a way to subscribe to App level notifications within the scope of the view.

    Very neat!
    Thanks,
    R.

Sign In or Register to comment.