【问题标题】:Co-routine killed after yield return UnityWebRequest.SendWebRequest协程在收益返回 UnityWebRequest.SendWebRequest 后被杀死
【发布时间】:2019-09-18 09:00:59
【问题描述】:

在我的应用程序中,多个协同程序正在并行下载文件,并且它们有可能尝试将相同的文件下载到相同的路径 - 导致冲突和访问异常。我尝试使用静态集合 private static List<string> filesBeingDownloadedNow = new List<string>(); 来解决这个问题,该集合包含当前正在下载的文件的 filePath。

看起来像这样:

private static List<string> filesBeingDownloadedNow = new List<string>();

private IEnumerator DoDownloadCoroutine(string url, string filePath)
{
    filesBeingDownloadedNow.Add(filePath);
    var webRequest = new UnityWebRequest(url)
    {
        method = UnityWebRequest.kHttpVerbGET,
        downloadHandler = new DownloadHandlerFile(filePath)
        {
            removeFileOnAbort = true
        }
    };
    yield return webRequest.SendWebRequest();
    if (webRequest.isNetworkError || webRequest.isHttpError)
    {
        // do something if the file failed to download
    }
    else
    {
        // do something with the downloaded file
    }
    filesBeingDownloadedNow.Remove(filePath);
}

和其他地方:

if (filesBeingDownloadedNow.Contains(filePath))
{
    // start another coroutine which simply waits until 
    // filesBeingDownloadedNow doesn't contain filePath anymore 
    // and then works with the downloaded file 
    // as if this coroutine downloaded it itself
}
else
{
    StartCoroutine(DoDownloadCoroutine(url, filePath));
}

现在,这完全可以正常工作,但在极少数情况下,运行此协程的游戏对象可能会在文件下载协程完成之前被销毁。所以,yield return webRequest.SendWebRequest(); 将是这里调用的最后一个东西,既不能检查成功也不能检查失败,并且永远不会到达 filesBeingDownloadedNow.Remove(filePath);。

我对此进行了测试,似乎UnityWebRequest.SendWebRequest 仍会完整下载文件 - 但是,它何时结束或文件是否成功下载尚不清楚。它只是“迷失在以太中”。

我可以简单地尝试围绕 File.Open 进行异常处理,或者我想对下载的文件执行任何其他操作(实际上无论如何都应该这样做)。但是,UnityWebRequest 不允许处理异常,因为它是用yield return 调用的。

即使在调用webRequest.SendWebRequest(); 之前,我使用if (File.Existst(filePath)) 检查文件是否已经存在,如果2 个不同的UnityWebRequest 尝试几乎同时下载同一个文件,这也无济于事。可能是第一个 UnityWebRequest 刚刚连接到服务器,还没有在 filePath 创建文件,第二个协程会看到该文件不存在并假设现在没有人在下载它,并且仍然尝试使用 webRequest.SendWebRequest(); 下载,导致冲突和我无法处理的异常。

所以基本上,有多种因素的组合:

  • 协同例程可以在任何时候被静默地杀死,不需要像“finally”这样的清理工作或在它被杀死之前做任何事情
  • UnityWebRequest.SendWebRequest 不允许处理异常 (?)
  • 下载的文件实际出现在该位置之前的延迟导致无法检查文件是否已被下载

坦率地说,我也很不喜欢这里的静态列表,但似乎由于文件无论如何都是全局资源,因此将其设为非静态并不能解决任何问题。无论我对该列表做什么,UnityWebRequest.SendWebRequest 都会继续自行下载文件。

【问题讨论】:

  • 为什么你的gameObject首先被销毁了?您可以跟踪正在进行的下载,并在OnDestroy 中发出警告/处理未完成的下载。或者作为替代方案,不要让这个特定的游戏对象处理下载/例程,而是将其传递给永远不会被破坏的专用管理器类

标签: c# unity3d coroutine


【解决方案1】:

你从来没有等待下载处理程序完成,如果你在它完成之前尝试使用它有时会导致奇怪的行为,最好在处理程序isDone标志为真之前产生null,即它完成处理下载的数据,取决于其类型。

是的,请求完成下载,但有时处理程序没有完成它的“处理”,在下载完成的同一时刻。

UnityWebRequest webRequest = UnityWebRequest.Get(url);
webRequest.downloadHandler = new DownloadHandlerFile(filePath) { removeFileOnAbort = true };

// 1) Wait until the download is done.
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError || webRequest.isHttpError)
{
    // do something if the file failed to download
}
else
{
    // 2) Wait until the handler itself finishes.
    while (!webRequest.downloadHandler.isDone)
        yield return null;

    // Now you can safely do something with the downloaded file
}

【讨论】:

    猜你喜欢
    • 2017-08-05
    • 2019-06-20
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多