【发布时间】:2015-09-16 08:57:40
【问题描述】:
我正在编写一段代码,从 MySQL 获取一条信息并将其显示在 UI 上。问题是,程序不会等待 MySQL 查询完成,而是直接显示变量(这是空的,因为查询的结果没有按时完成)
我的代码的大致轮廓是:
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 参数似乎会产生无限循环。但我会尝试一些使用它的方法。感谢您的建议
-
如果你使用: while(!web.isDone) 会怎样,所以当它完成时它会为真并退出循环。
-
我应该在循环中放什么?我只是在等待查询完成
-
while(!web.isDone){ //这将循环直到您的查询完成}if(web.Error == nulll){Debug.log("No errors to display")}else {Debug.Log("Errors on web....")} 我认为这可以工作,现在无法测试,因为我在工作,但是当我有机会时,我可以尝试...跨度>
标签: c# mysql unity3d coroutine