我正在制作一段代码,它从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并退出循环。 –