【问题标题】:PCLStorage problems with Xamarin.iOSXamarin.iOS 的 PCLStorage 问题
【发布时间】:2017-02-07 15:35:42
【问题描述】:

我在 Xamarin 中编写了我的应用程序,首先针对 Android 进行了测试,然后针对 iOS 进行了测试。在 Android 上一切正常,而不是在 iOS 上。我的问题与PCLStorage有关,读取文件内容时失败,因为GetFileAsync结果是null,而不是CheckExistsAsync结果。

我的代码是:

public static async Task loadMyFile(Func<bool, Task> result) {
    IFolder rootFolder = FileSystem.Current.LocalStorage;
    await rootFolder.CheckExistsAsync("myfile.txt").ContinueWith(async (checkExistsTask) => {
        if (checkExistsTask.Result == ExistenceCheckResult.FileExists) {
            await rootFolder.GetFileAsync("myfile.txt").ContinueWith(async (getFileTask) => {
                try
                {
                    await getFileTask.Result.ReadAllTextAsync().ContinueWith(async (readTextTask) => {
                        try
                        {
                            if (!string.IsNullOrEmpty(readTextTask.Result))
                            {
                                doWork(readTextTask.Result);
                                await result(true);
                                return;
                            }
                            else
                            {
                                await result(false);
                                return;
                            }
                        }
                        catch (Exception e)
                        {
                            await result(false);
                            return;
                        }
                    });
                }
                catch (Exception e)
                {
                    await result(false);
                    return;
                }
            });
        } else {
            await result(false);
            return;                 
        }
    });
}

有什么想法吗? 谢谢。

【问题讨论】:

    标签: ios xamarin storage


    【解决方案1】:

    我不知道你为什么在等待任务时在这里使用ContinueWith

    我可能会这样写代码:

    public static async Task loadMyFile(Func<bool, Task> result) 
    {
        IFolder rootFolder = FileSystem.Current.LocalStorage;
        var exists = await rootFolder.CheckExistsAsync("myfile.txt");
        if (exists == ExistenceCheckResult.FileExists) 
        {
            var file = await rootFolder.GetFileAsync("myfile.txt");
            try
            {
                var text = await file.ReadAllTextAsync();
                if (!string.IsNullOrEmpty(text))
                {
                    doWork(text);
                    await result(true);
                    return;
                }
                else
                {
                    await result(false);
                    return;
                }
            }
            catch (Exception e)
            {
                await result(false);
                return;
            }
        } else {
            await result(false);
            return;                 
        }
    }
    

    这更容易阅读,并且您可以摆脱不必要的 try/catch。

    您可能希望将ConfigureAwait(false) 添加到您的任务中,这样您就不必进行太多上下文切换。

    您可以通过摆脱回调来大大简化您的方法,并且只返回一个布尔值作为您的任务的结果:

    public static async Task<bool> loadMyFile() 
    {
        IFolder rootFolder = FileSystem.Current.LocalStorage;
        var exists = await rootFolder.CheckExistsAsync("myfile.txt");
        if (exists == ExistenceCheckResult.FileExists) 
        {
            var file = await rootFolder.GetFileAsync("myfile.txt");
            try
            {
                var text = await file.ReadAllTextAsync();
                if (!string.IsNullOrEmpty(text))
                {
                    doWork(text);
                    return true;
                }
            }
            catch (Exception e)
            {
            }
        }
    
        return false;
    }
    

    【讨论】:

    • 你是对的。现在没有 ContinueWith 完美工作:) 谢谢
    • 回调对我来说很重要还有另一个原因...非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多