While asynchronous queries are essential to prevent the UI from hanging up while waiting for the data, sometimes it is essential that the data is retrieved before the UI is displayed. I am creating an object and then passing that object to a dialog box, unfortunately there is some processing being done on the object before it can be passed to the dialog box. The object is not being fully created before the dialog box is displayed which causes the program to crash.
How can I pause the app and keep the dialog box from appearing until my object is created and populated with some default values?
Here is what is happening? the user clicks on an add button and the object is created, the next available reference # is retrieved from the database, updated by 1 and then saved back to the database. The number is assigned to a field in the newly created object and returned back to the calling method. The calling method calls the dialog box and passes the newly created object to the viewmodel of the dialog box.
Here is my code from the viewmodel that is calling the create object and popping up the dialog and the repository method
public JobHistory newJob;
private void AddJob(object parameter){
CreateJobHistory(CurrentMember);
I want to put a pause here until the newJob variable is not null and a valid object.
this.IsInEditing = true;
JobHistoryEditor.Open(newJob,
() => {
// Ok callback
this.Jobs.Add(SelectedJob);
this.SelectedJob = newJob;
CloseEditSession();
},
CloseEditSession);
// Cancel
}
public void CreateJobHistory(Member member)
{
var coop = Coroutine.Start(() => JHRepository.CreateJobHistory(member));
coop.Completed += (sender, args) => {
if (args.CompletedSuccessfully){
newJob = coop.Result
as JobHistory;
}
else{
}
};
}
Here is my repository method
//Coroutine Iterator
public IEnumerable<INotifyCompleted> CreateJobHistory(Member member)
{
var localInfoOp = Manager.LocalInfos.ExecuteAsync();
yield return localInfoOp;
//Get first LocalInfo.NextJobHistRef and increment by 1
LocalInfo localInfos = localInfoOp.Results.First<LocalInfo>();
int nextRefNo = localInfos.NextJobHistRef;
localInfos.NextJobHistRef = nextRefNo + 1;
List<Entity> changedEntities = new List<Entity>();
changedEntities.Add(localInfos);
var saveOp = Manager.SaveChangesAsync(changedEntities);
saveOp.Completed += (sender, args) =>{
if (args.CompletedSuccessfully){
}
else
{
}
};
// ////Create a new JobHistory
JobHistory newJob = new JobHistory{
SocSecNo = member.SocSecNo,
RefNo = nextRefNo,
Member = member
};
yield return Coroutine.Return(newJob);
}