首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用ContinueWith或委托运行某些任务后,不运行RunOnUiThread

在使用ContinueWith或委托运行某些任务后,不运行RunOnUiThread
EN

Stack Overflow用户
提问于 2016-09-25 20:05:50
回答 1查看 204关注 0票数 0

我想在运行Task(从API获取一些数据)之后,在UI-thread中更改我的UI元素(隐藏进度条和为listview分配数据)。我找不到API的问题,所有情况下都会返回数据。有时在运行任务后执行代码(在RunOnUiThread中)变得无响应,特别是当我在调试模式下运行代码时没有任何断点。当我在调用Task之前捕获断点并继续运行时,一切正常。

代码语言:javascript
复制
protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            _userData = ServiceLocator.GetService<IAuthService>().GetUserData();
            _wallsViewPresenter = ViewPresenterHelper.CreateViewPresenter<WallViewPresenter, IWallView, WallActivity>(this);
            _listView = FindViewById<ListView>(Resource.Id.postList);

            progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar1);
            Task.Run(async () => {
                profile = await _wallsViewPresenter.GetProfile(int.Parse(_userData.Profile));
                WallModel wall = SerializationService.DeSerialize<WallModel>(profile.Wall);
                _posts = (List<PostModel>) (wall.Posts.ToList());                        
            }).ContinueWith(ar =>
            {
                RunOnUiThread(() => {
                    progressBar.Visibility = ViewStates.Gone;
                    _postListAdapter = new PostListAdapter(this, _posts);
                    _listView.Adapter = _postListAdapter;
                    SetListViewHeader();
                    _listView.AddHeaderView(_header);
                    FindViewById<TextView>(Resource.Id.details).Text = profile.Name;
                });
            });               
        }

带委派的版本:

代码语言:javascript
复制
        protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        _userData = ServiceLocator.GetService<IAuthService>().GetUserData();
        _wallsViewPresenter = ViewPresenterHelper.CreateViewPresenter<WallViewPresenter, IWallView, WallActivity>(this);
        _listView = FindViewById<ListView>(Resource.Id.postList);
        progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar1);
        Task.Run(async () => {
            profile = await _wallsViewPresenter.GetProfile(int.Parse(_userData.Profile));
            WallModel wall = SerializationService.DeSerialize<WallModel>(profile.Wall);
            _posts = (List<PostModel>) (wall.Posts.ToList());
            if (_posts.Count != 0)
            {
                DataPopulated?.Invoke(this, true);
            }
        });
        DataPopulated += (sender, e) => {
            RunOnUiThread(() => {
                progressBar.Visibility = ViewStates.Gone;
                _postListAdapter = new PostListAdapter(this, _posts);
                _listView.Adapter = _postListAdapter;
                SetListViewHeader();
                _listView.AddHeaderView(_header);
                FindViewById<TextView>(Resource.Id.details).Text = profile.Name;
            });
        };
    }
EN

回答 1

Stack Overflow用户

发布于 2016-09-26 08:49:35

ContinueWith是一个非常危险的低级应用编程接口,不应该在这里使用。而且,有了async/await,就不需要RunOnUIThread了。

也有可能Task.Run是不必要的,因为您正在调用的方法已经是异步的。

简化版:

代码语言:javascript
复制
protected override async void OnCreate(Bundle savedInstanceState)
{
  base.OnCreate(savedInstanceState);
  _userData = ServiceLocator.GetService<IAuthService>().GetUserData();
  _wallsViewPresenter = ViewPresenterHelper.CreateViewPresenter<WallViewPresenter, IWallView, WallActivity>(this);
  _listView = FindViewById<ListView>(Resource.Id.postList);

  progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar1);
  profile = await _wallsViewPresenter.GetProfile(int.Parse(_userData.Profile));
  WallModel wall = SerializationService.DeSerialize<WallModel>(profile.Wall);
  _posts = (List<PostModel>) (wall.Posts.ToList());                        

  progressBar.Visibility = ViewStates.Gone;
  _postListAdapter = new PostListAdapter(this, _posts);
  _listView.Adapter = _postListAdapter;
  SetListViewHeader();
  _listView.AddHeaderView(_header);
  FindViewById<TextView>(Resource.Id.details).Text = profile.Name;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39686597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档