【问题标题】:Not all code paths return a value when using Task.ContinueWith使用 Task.ContinueWith 时,并非所有代码路径都返回值
【发布时间】:2015-08-19 13:22:10
【问题描述】:

所以我有这个函数,我在所有 if-else 中都有一个返回但仍然得到编译错误:

并非所有代码路径都返回值

public async Task<bool> DeletePost(string update_id, string authId)
{
    if (Utility.NetworkStatus.HasInternetAccess)
    {
        await APIs.DeletePost.DeletePostAPI(update_id, authId).ContinueWith((t) =>
        {
            if (t.Status == TaskStatus.RanToCompletion)
            {
                if (t.Result != null)
                {
                    return t.Result.status == 200;
                }
                else
                {
                    return false;
                    //empty result, API failed
                    //not implemented
                }
            }
            else
            {
                return false;
                //task failed 
                //not implemented
            }
        });
    }
    else
    {
        return false;
        //no network
        //not implemented
    }
}

谁能告诉我我做错了什么?

【问题讨论】:

  • return await APIs.DeletePost.DeletePostAPIreturn false 在方法的末尾

标签: c# .net windows-runtime windows-phone-8.1 task


【解决方案1】:

是的。你不返回DeletePostAPI继续的结果:

public async Task<bool> DeletePost(string update_id, string authId)
{
    if (Utility.NetworkStatus.HasInternetAccess)
    {
        return await APIs.DeletePost.DeletePostAPI(update_id, authId).ContinueWith((t) =>
        {
            if (t.Status == TaskStatus.RanToCompletion)
            {
                if (t.Result != null)
                {
                    return t.Result.status == 200;
                }
                else
                {
                    return false;
                    //empty result, API failed
                    //not implemented
                }
            }
            else
            {
                return false;
                //task failed 
                //not implemented
            }
        });
    }
    else
    {
        return false;
        //no network
        //not implemented
    }
}

【讨论】:

  • 那么这个新的回报什么时候执行?考虑到我的功能流程
  • @Rishabh876 在DeletePostAPI 完成后,其延续完成后,您从延续返回的值是从DeletePost 返回的
  • 哦该死的,现在我明白了。我真是太傻了。谢谢
【解决方案2】:

如果您完全删除 ContinueWith 并改用更现代的 await,您的代码会变得简单得多:

public async Task<bool> DeletePost(string update_id, string authId)
{
  if (Utility.NetworkStatus.HasInternetAccess)
  {
    try
    {
      var result = await APIs.DeletePost.DeletePostAPI(update_id, authId);
      if (result != null)
      {
        return result.status == 200;
      }
      else
      {
        return false;
        //empty result, API failed
        //not implemented
      }
    }
    catch
    {
      return false;
      //task failed 
      //not implemented
    }
  }
  else
  {
    return false;
    //no network
    //not implemented
  }
}

【讨论】:

    【解决方案3】:

    您需要返回等待的返回值。所以:

    bool result = await APIs.DeletePost.DeletePostAPI(update_id, authId).ContinueWith((t) =>
                    {
                        if (t.Status == TaskStatus.RanToCompletion)
                        {
                            if (t.Result != null)
                            {
                                return t.Result.status == 200;
                            }
                            else
                            {
                                return false;
                                //empty result, API failed
                                //not implemented
                            }
                        }
                        else
                        {
                            return false;
                            //task failed 
                            //not implemented
                        }
                    });
    return result;
    

    【讨论】:

      猜你喜欢
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2013-10-06
      • 2016-02-14
      • 2014-04-02
      相关资源
      最近更新 更多