如何干净地封装和iOS中依次执行一系列的后台任务? [英] How to cleanly encapsulate and execute in sequence a series of background tasks in iOS?

查看:140
本文介绍了如何干净地封装和iOS中依次执行一系列的后台任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序包括后端服务器,与必须在后台进行了很多交易。许多这些交易需要code许多同步位运行。

My app includes a back-end server, with many transactions which must be carried out in the background. Many of these transactions require many synchronous bits of code to run.

例如,做一个查询,使用结果做另一个查询,创建一个新的后端对象,然后在前台给新的对象返回引用到一个视图控制器对象,这样的UI可以更新。

For example, do a query, use the result to do another query, create a new back-end object, then return a reference to the new object to a view controller object in the foreground so that the UI can be updated.

一个更具体的情况是,进行序列的AJAX为了呼吁,类似这个问题,但iOS中。

A more specific scenario would be to carry out a sequence of AJAX calls in order, similar to this question, but in iOS.

这一系列任务确实是一个统一的作品。我没有发现在iOS中,让我干净code这个序列作为工作单位现有设施。同样,我没有看到一个方法来提供对工作单位,将跨越异步任务的顺序提供一个一致的环境。

This sequence of tasks is really one unified piece of work. I did not find existing facilities in iOS that allowed me to cleanly code this sequence as a "unit of work". Likewise I did not see a way to provide a consistent context for the "unit of work" that would be available across the sequence of async tasks.

推荐答案

最近,我不得不做一些JavaScript和必须学会使用无极概念,是常见的JS。我意识到,我可以适应这个想法到iOS和Objective-C。结果是这里 Github上。有文档,code和单元测试。

I recently had to do some JavaScript and had to learn to use the Promise concept that is common in JS. I realized that I could adapt this idea to iOS and objective-C. The results are here on Github. There is documentation, code and unit tests.

一个承诺应该被认为是一种承诺在未来某个时间,结果对象(ID)或一个错误对象(NSError)返回块。一个承诺对象被创建重新present异步结果。异步code将结果传递给无极,然后承诺时间表和运行一个块来处理结果或错误。

A Promise should be thought of as a promise to return a result object (id) or an error object (NSError) to a block at a future time. A Promise object is created to represent the asynchronous result. The asynchronous code delivers the result to the Promise and then the Promise schedules and runs a block to handle the result or error.

如果你熟悉的JS承诺,你会立刻意识到了iOS版本。如果没有,请查看自述文件和参考。

If you are familiar with Promises on JS, you will recognize the iOS version immediately. If not, check out the Readme and the Reference.

这篇关于如何干净地封装和iOS中依次执行一系列的后台任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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