后台任务完成后如何向主UI线程指示? (performSelectorInBackground) [英] how to indicate to main UI thread when background task has completed? (performSelectorInBackground)

查看:224
本文介绍了后台任务完成后如何向主UI线程指示? (performSelectorInBackground)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当后台任务完成后,如何在iPhone应用程序IOS程序中获取主UI线程的指示?

How can I get an indication to main UI thread in an iPhone app IOS program, when background task has completed?

背景


  • 我正在尝试根据概念设置加载指标在如何添加UIActivityIndi​​cator到iphone应用程序中的启动画面?

  • 在AppDelete中使用performSelectorInBackground来加载模型数据

  • 因此我需要在RootViewController中以某种方式告知数据何时在后台完成加载,以便它可以(a)用数据更新tableview并(b)删除任何活动指示符

  • 我假设在这里做的事情如下:


    • 在App Delegate didFinishLaunchingWithOptions传递模型数据加载到后台

    • AppDelegate加载RootViewController并立即设置活动指示符

    • 一旦数据加载到后台,它就会不知何故必须将此指示回RootViewController(?这个问题的原因)它已经完成了

    • 第二个问题可能也是当后台任务确实完成它的时候,RootviewController如何检查UI确实得到了设置(带有活动指示符等)它试图禁用活动指示器

    • I'm trying to setup a loading indicator per the concept at How to add a UIActivityIndicator to a splash screen in a iphone application?
    • was going to in AppDelete use "performSelectorInBackground" to load up the model data
    • what I need therefore is in the RootViewController some way of telling when the data has finished loading in the background so that it can (a) update the tableview with the data and (b) remove any activity indicator
    • I'm assuming way to do things here is as followings:
      • in App Delegate didFinishLaunchingWithOptions pass off Model data loading to background
      • AppDelegate loads up RootViewController and it immediately setup up an activity indicator
      • once data is loaded in background, it somehow has to indicate this back to the RootViewController (? reason for this question) that it's finished
      • 2nd question is probably also when background task does inidicate its finished, how can the RootviewController check that the UI did get setup (with activity indicator etc) before it tries to disable the activity indicator

      推荐答案

      您可以使用 -performSelectorOnMainThread:withObject:waithUntilDone:从后台选择器回调到主线程,如下所示:

      You can call back into the main thread from your background selector using -performSelectorOnMainThread:withObject:waithUntilDone: like so:

      - (void)loadModel
      {
          // Load the model in the background
          Model *aModel = /* load from some source */;
      
          [self setModel:aModel];
          [self performSelectorOnMainThread:@selector(finishedLoadingModel) withObject:nil waitUntilDone:YES];
      }
      
      - (void)finishedLoadingModel
      {
          // Notify your view controller that the model has been loaded
          [[self controller] modelLoaded:[self model]];
      }
      

      更新:这是一种更安全的方法将检查 -finishedLoadingModel 以确保您在主线程上运行:

      Update: an even safer way to do this would be to check in -finishedLoadingModel to make sure you're running on the main thread:

      - (void)finishedLoadingModel
      {
          if (![NSThread isMainThread]) {
              [self performSelectorOnMainThread:_cmd withObject:nil waitUntilDone:YES];
          }
          // Notify your view controller that the model has been loaded
          [[self controller] modelLoaded:[self model]];
      }
      

      这篇关于后台任务完成后如何向主UI线程指示? (performSelectorInBackground)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆