【问题标题】:What does the forceRefresh parameter do in a method that only calls await Task.FromResult(items)?forceRefresh 参数在只调用 await Task.FromResult(items) 的方法中有什么作用?
【发布时间】:2019-10-21 11:21:31
【问题描述】:

我正在尝试创建一个基于 Visual Studio 提供的选项卡式页面模板的 Xamarin Forms 应用程序。在查看模板中的代码时,他们使用通用的IDataStore<T> 接口来定义以下方法:

public interface IDataStore<T> {
Task<IEnumerable<T>> GetItemsAsync(bool forceRefresh = false); }

然后他们定义了MockDataStore类,该类通过以下方式实现IDataStore&lt;Item&gt;接口:

public class MockDataStore : IDataStore<Item> {
    readonly List<Item> items;

    public MockDataStore() {
        items = new List<Item>() { 
            new Item { Id = Guid.NewGuid().ToString(), Text = "First item", Description="This is an item description." },
            new Item { Id = Guid.NewGuid().ToString(), Text = "Second item", Description="This is an item description." } } 
};

    public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false) => await Task.FromResult(items);

我不知道forceRefresh 参数在做什么。我知道他们使用空参数列表调用GetItemsAsync,并使用true 作为参数调用它一次。我在 MSDN 上找不到任何关于 Task.FromResults 方法中使用的 forceRefresh 的文档。

Item 类没有什么特别之处,它是一个标准的公共类,具有三个公共字符串自动实现的 get/set 属性,名为 IdTextDescription

这是一个 Xamarin Forms 模板,因此它们在启动应用程序时将 MockDataStore 类注册为依赖服务,并且它们仅通过 IDataStore&lt;Item&gt; 的公共 get-only 实例引用方法,如下所示:

public class BaseViewModel : INotifyPropertyChanged {
    public IDataStore<Item> DataStore => DependencyService.Get<IDataStore<Item>>(); 

然后在加载命令方法中他们引用GetItemsAsync 调用:

var items = await DataStore.GetItemsAsync(true);

我的猜测是,如果forceRefresh 为假,那么如果某些情况为真,则获取项目的调用实际上不会发生,例如传递给Task.FromResult(items) 的项目不为空。不知道forceRefresh是和手机列表视图的刷新指示有关,还是只是指刷新项目列表本身。

任何人可以提供的澄清或参考都会有所帮助。如果您需要我提供任何其他信息来帮助您找到答案(我希望您会这样做,因为这是我的第一篇文章),我很乐意编辑问题并提供。

【问题讨论】:

  • public async Task&lt;IEnumerable&lt;Item&gt;&gt; GetItemsAsync(bool forceRefresh = false) =&gt; 在该特定实例中,forceRefresh 参数绝对没有作用
  • forceRefresh 方法经常在检索数据的成本很高时使用 - 因此涉及某种形式的缓存。当forceRefresh 作为true 传递时,它基本上意味着“绕过缓存,请去给我获取真实数据”。请注意,这不是它在这里所做的(在此代码中,参数被忽略)。我只是指出该名称的参数通常的用途。
  • IDataStore&lt;Item&gt;any 实现是否实际使用了该参数?
  • @mjwills 根本没有IDataStore&lt;T&gt; 的其他实现。它来自内置的 Visual Studio 模板,所以也许他们只是将其放入以展示如何在另一个实现中使用它。

标签: c# visual-studio xamarin.forms


【解决方案1】:

forceRefresh 在这种情况下不会做任何事情。

很可能实现IDataStore&lt;Item&gt;real 类使用forceRefresh 使用它来使方法 返回缓存值并获取新数据。

当每次调用都需要很长时间获取新数据时使用此技术。发出 Web 请求或计算某些内容可能需要时间(或 CPU)。另一种情况是刷新数据可能不会一直有效,本地的,甚至是陈旧的副本可能总比没有任何东西要好。

这是一个人为延迟的例子。

public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false) 
{
    if( cache == null || forceRefresh == true)
    {
        // Simulate a long call (an web api request, for example)
        await Task.Delay(TimeSpan.FromSeconds(10))
        cache = new List<Item>{new Item() {Name = "One"}, new Item() {Name = "One"}};
    }

    return cache;
}

示例

代码

public interface IDataStore<T>{ Task<IEnumerable<string>> GetItemsAsync(bool forceRefresh = false); }

public class DataStore : IDataStore<string>
{
    List<string> cache;

    public async Task<IEnumerable<string>> GetItemsAsync(bool forceRefresh = false)
    {
        if (cache == null || forceRefresh == true)
        {
            await Task.Delay(TimeSpan.FromSeconds(2)); // Simulate a long call
            cache = new List<string> { "One", "Two" };
        }

        return cache;
    }

    public static async Task Main(string[] args)
    {
        var i = 0;
        var dataStore = new DataStore();

        var sw = Stopwatch.StartNew();
        await dataStore.GetItemsAsync(); //Slow
        Console.WriteLine($"Call {i++} took {sw.ElapsedMilliseconds}ms");

        sw.Restart();
        await dataStore.GetItemsAsync(); //Fast
        Console.WriteLine($"Call {i++} took {sw.ElapsedMilliseconds}ms");


        sw.Restart();
        await dataStore.GetItemsAsync(forceRefresh: true); //Fast
        Console.WriteLine($"Call {i++} took {sw.ElapsedMilliseconds}ms");
    }
}

输出

Call 0 took 2021ms
Call 1 took 0ms
Call 2 took 2004ms

【讨论】:

    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多