IPhone equivalent to Application.DoEvents ();
iPHone: We're using MonoTouch, but Obj-C's answers are ok.
My solid color domain object takes a while to fetch all the data so that it fetch internally on the stream.
I need to tell the UI that the domain is done. I am currently doing this. Is there a better way? In WinForms, I would call Application.DoEvents () instead of Thread Sleep.
PlanDomain domain = PlanDomain.Instance ();
while (domain.IsLoadingData)
{
Thread.Sleep (100); //this is the main UI thread
}
TableView.Hidden = false;
TableView.Source = new TableSource (this);
TableView.ReloadData ();
a source to share
It would be better to have something derived from the AlertView (download dialog) if you don't want to do a background download.
Then the task is executed in a separate one Thread
, which you connect and then close the dialog. Here's an example here but does not contain threads, however it is trivial to add:
public class LoadingView : UIAlertView
{
private UIActivityIndicatorView _activityView;
public void Show(string title)
{
Title = title;
Show();
// Spinner - add after Show() or we have no Bounds.
_activityView = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge);
_activityView.Frame = new RectangleF((Bounds.Width / 2) - 15, Bounds.Height - 50, 30, 30);
_activityView.StartAnimating();
AddSubview(_activityView);
// I haven't tested this, it should display the dialog still.
Thread thread = new Thread(DoWork);
thread.Start();
thread.Join();
Hide();
}
private void DoWork()
{
PlanDomain domain = PlanDomain.Instance ();
while (domain.IsLoadingData)
{
// Maybe replace domain.IsLoadingData with an Event for when it finishes.
// And then use a Mutex (WaitHandle) to communicate back
}
}
public void Hide()
{
DismissWithClickedButtonIndex(0, true);
}
}
a source to share
I would see if you can use performSelectorOnMainThread: move your Tableview code to your own method and return it when you're done with loading data.
Delegates are also useful in this scenario - get your PlanDomain to call back the delegate function when it's done.
Else, if you want something a little more detached, look at using NSNotification s. They are most useful when the code you are writing does not need to know or care about what happens when it ends - it just needs to tell the world that it has done its job. (Most useful when there is potentially more than one thing that might be interested in action when completed.
In any of them you don't want to sleep - just let runloop handle it.
a source to share