GCD:UIViews上的两个并行动画 [英] GCD: two parallel animations on UIViews

查看:80
本文介绍了GCD:UIViews上的两个并行动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自这篇文章,因为讨论与主要问题相去甚远. 无法理解的GCD异步方法的行为

I came from this post, as the discussion is too much deviated from the main question. Behavior of GCD async method not understandable

请帮助

我的iPad应用程序具有两个并排的TableViews. 这两个必须从两个不同的Web服务实时加载数据. 我想要两个线程-每个线程专用于一个tableview. 由于dispatch_get_main_queue仅会给我一个序列号,因此我将无法真正实时更新表.而且,它还涉及更新UI元素,我只需要对主队列(即串行队列)执行线程处理-无法并行获取.

My iPad application has two side by side TableViews. These two have to load the data in real time from two different web services. I would like to have two threads - each dedicated to one tableview. As dispatch_get_main_queue would give me only a serial one, I will not be really able to update the tables in real time. And, also it involves updating UI elements, I have to only do the threading on the main queue, which is serial - making the parallel fetch not possible.

有人可以补充您的想法,让我如何让两个单独的线程同时运行并更新UI元素?

Can someone please add your thoughts and let me how can I have two separate threads running concurrently and updating the UI elements?

推荐答案

您问:

有人可以补充您的想法,让我如何让两个单独的线程同时运行并更新UI元素?

Can someone please add your thoughts and let me how can I have two separate threads running concurrently and updating the UI elements?

您可以简单地让那些线程在后台线程中执行其请求并进行解析,然后让它们将模型和UI更新分派回主队列.

You can simple have those thread do their requests and parsing in the background threads, and then have them dispatch the model and UI updates back to the main queue.

例如,您可以执行以下操作:

For example, you could do something like:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   // issue first web service request

   // when done, update the UI

   dispatch_async(dispatch_get_main_queue(), ^{
       // update the model here

       // also issue the `reloadData` (or whatever table updates you want) 
       // for the first table view
   });
});

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   // issue second web service request

   // when done, update the UI

   dispatch_async(dispatch_get_main_queue(), ^{
       // update the model here

       // also issue the `reloadData` (or whatever table updates you want) 
       // for the second table view
   });
});

这两个将并发运行(因为所有全局队列都是并发队列),但是UI/模型更新将被分派到主队列,这将确保所有内容保持同步,UI更新成功发生,等等.因此,您喜欢后台队列提供并发性,但是您将在需要的地方利用串行主队列.

These two will run concurrently (because all global queues are concurrent queues), but UI/model updates will be dispatched to the main queue, which will ensure everything stays synchronized, that UI updates happen successfully, etc. Thus you enjoy the concurrency offered by background queues, but you'll take advantage of the serial main queue where you need to.

很显然,以上假设假设您正在将网络请求分派到某个后台队列(并且我们将使用默认的全局队列),但是发出异步网络请求的确切机制可能相差很大(并且可能不涉及任何调度).如果您需要特定的顾问,则应该准确共享如何向Web服务发出请求(例如AFNetworking?NSURLConnection(如果是NSURLConnection,则是便捷方法或基于委托的方法之一)?NSURLSession? ),因为实施细节会根据您提出请求的方式而有所不同.

Clearly, the above assumes that you're dispatching the network requests to some background queue (and we'll use the default global queue), but the precise mechanism for issuing asynchronous network requests can vary greatly (and might not involve any dispatching at all). If you need specific counsel, you should share precisely how you're issuing the requests to your web services (e.g. AFNetworking? NSURLConnection (and if NSURLConnection, one of the convenience methods or delegate based approach)? NSURLSession?), as the implementation details will vary based upon how you're making the request.

但是想法很简单:异步执行网络服务请求,然后确保在请求完成后,对主队列执行生成的模型和UI更新.这样,您可以享受请求的并发性,但是UI更新会在主队列上进行.

But the idea is simple: Perform the network service requests asynchronously, and then make sure that upon the completion of the request, you perform the resulting model and UI updates on the main queue. This way, you enjoy the concurrency of the requests, but the UI updates happen on the main queue.

这篇关于GCD:UIViews上的两个并行动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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