在主文件MainPage中,我有一个名为OnAppearing的方法,包含两个函数。
MainPage.cs
MainPage.cs
protected override void OnAppearing()
{
DataBaseService.checkNoteAlarmTimeActive();
this.loadNotes();
base.OnAppearing();
}
很遗憾,方法loadNotes()在方法DataBaseService.checkNoteAlarmTimeActive();结束之前被触发了。这会在我的应用程序中出现一些问题。 怎样才能使this.loadNotes()等待前一个函数结束后再执行呢?
谢谢! 具有以下方法的类DataBaseService:
public static async void checkNoteAlarmTimeActive()
{
List<Note> notes = await getTimeActiveNotes();
foreach(Note note in notes)
{
if(note.AlarmTime < DateTime.Now)
{
note.AlarmTimeActive = false;
updateRecord(note);
}
}
}
public static async Task<List<Note>> getTimeActiveNotes()
{
var notes = await _dbConnect.Table<Note>().Where(i => i.AlarmTimeActive == true).ToListAsync();
return notes;
}
async
/await
,这会比promises/thenables产生更干净的代码。 - Stephen Cleary