我正在製作一段代碼,它從MySQL獲取一條信息並將其顯示在UI上。問題是,程序不會等待MySQL查詢完成並直接顯示變量(由於查詢結果沒有按時完成,因此該變量爲空)如何在繼續之前等待StartCoroutine()完成
粗略提綱我的代碼是:
bool notYetDone = true;
StartCoroutine(query(web));
IEnumerator query (WWW web){
yield return web;
while(notYetDone == true){
yield return new WaitForSeconds(1f);
if(web.error == null){
//no problems with the query, some code here
notYetDone = false;
} else if (web.error != null){
//some code here for handling errors
} else {
Debug.Log("i dont really know why we reached here");
}
}
}
東西我還注意到的是,它似乎改變的notYetDone
值,並立即結束循環。我的代碼有問題嗎?提前致謝。
嘗試使用web.isDone而不是notYetDone == true –
直接用web.isDone替換while參數似乎會產生無限循環。但生病嘗試使用這種方法。感謝您的建議 – ryuuuuuusei
如果您使用:while(!web.isDone),那麼當它完成時它將是true並退出循環。 –