在 iPhone 上设计和启动冗长、依赖的异步进程的最佳方法是什么?
我正在设计一款 iPhone 应用程序。我有一个非常紧张的过程要开始,在高层定义如下:
- 用户从 UITableView 中为 inApp 选择项目 购买。
- 用户在 UI 中确认购买。
- 异步进程去App Store检查是否有item 之前已经购买过。如果不, 该物品已购买。 iPhone 然后通知应用程序该项目 购买成功。 (如果是 之前购买的应用程序 不关心并继续执行步骤 4。)
- 然后应用程序会转到我的私人服务器下载所购买商品的数据。
- 然后应用程序将数据导入 CoreData。
当然,在该过程的每个步骤中,都会向用户报告错误,并且整个过程会停止。
我有一个名为 ActivityIndicatorController 的类,它为用户提供了一个很好的 UIActivityIndicator 以及一些指示正在发生的情况的文本。在该过程的每个步骤中,activityIndicator 上的标签必须更新为新状态。
问题/担忧:
从设计和编码的角度来看,最简单的事情似乎是对所有步骤使用一个大的 NSOperation。然而,我担心这可能会妨碍我以后的发展。我不想走上错误的道路,然后不得不撕掉一堆代码。
我的直觉告诉我,最好实现三个单独的 NSOperations(OpInAppPurchase、OpDownload、OpImport)。然而,每个后续操作不仅依赖于之前的操作,而且如果其中一个操作失败,则根本不应该调用 NSOperations 的其余部分。但是,我不确定如何设计/编码。
任何帮助表示赞赏。
编辑:
感谢迄今为止大家的帮助。对于我想要完成的任务,我不希望用户取消操作。但是,我确实希望应用程序能够取消后续操作。我没有意识到并且只是通过反复试验(即“duh”时刻)才发现的是,当在单独的进程中启动操作时,启动的任何子操作也会在同一个单独的进程中执行。
在阅读了下面链接的 Apple 文章后,我发现,对于我现在的需求,NSInvocableOperation 正是我所需要的,并且使我不必为每个进程创建单独的 NSOperation 对象。如果失败,它将使用 NSError 对象对主线程进行回调,或者在成功时对主线程进行不同的回调。
也许我确实希望用户将来能够取消一些其他进程,并且我会将 NSOperation 与其他事件一起使用。
I am working on a design of an iPhone app. I have a pretty intense process to kick off, defined at a high level as follows:
- User selects item from UITableView for inApp
purchase. - User confirms purchase in UI.
- Asynchronous process goes to App Store to check to see if item
has been purchased before. If not,
the item is purchased. The iPhone
app is then notified that the item
purchase was successful. (If it was
purchased previously, the app
doesn't care and proceeds to step 4.) - The app then goes to my private server to download the data for the item purchased.
- The app then imports the data into CoreData.
At each step in the process, of course, errors are reported to the user and the entire process stops.
I have a class called activityIndicatorController that presents a nice UIActivityIndicator for the user along with some text indicating what is going on. At each step in the process, the label on the activityIndicator must be updated with new status.
Question/Concern:
It seems from a design and coding standpoint, the EASIEST thing to do would be to use one big NSOperation for all steps. However, I'm concerned that this may hamstring me later on. I don't want to go down the wrong path and then have to tear out a bunch of code later.
My gut tells me it would be BETTER to implement three separate NSOperations (OpInAppPurchase, OpDownload, OpImport). However, each subsequent operation is not only dependent on the one before it, but if one fails, the rest of the NSOperations should not even be called at all. But, I'm not sure how to design/code this.
Any help is appreciated.
EDIT:
Thanks for everyone's help so far. For what I am trying to accomplish, I do not want the user to cancel the operation. However, I DO want the app to be able to cancel subsequent operations. What I failed to realize and only just discovered by trial and error (i.e. a "duh" moment) is that when an operation is kicked off in a separate process, any child-operations that are kicked off are also performed in the same separate process.
After reading the Apple article linked to below, I discovered that for my needs right now, NSInvocationOperation does exactly what I need and saves me from having to create separate NSOperation objects for each process. It will then do a callback with an NSError object to the main thread if it fails or a different callback to the main thread when successful.
Perhaps I will have some other processes that I do want the user to be able to cancel in the future and I will use NSOperation with the other events.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过将每个步骤添加到 NSOperationQueue 中,您可以在继续之前进行检查以确保该步骤失败。请参阅这篇关于一般方法的文章。
如果操作遇到问题,您还可以发起并响应来自 NSOperation 的取消请求。请参阅此 响应取消事件 “nofollow”>文章。
响应取消事件:
By adding each step into an
NSOperationQueue
You can check to ensure that step failed before continuing. See this article on the general approach.You can also initiate and respond to cancellation requests from the NSOperation if the operation encounters a problem. See Responding to Cancellation Events in this article.
Responding to Cancellation Events:
使用 NSOperationQueue。将每个任务封装在其自己的 NSOperation 中。使用
addDependency:
设置依赖项。“当一个操作失败时,如何防止执行其他操作?”,您问。
好吧,首先,确保您的 NSOperation 对象是好公民,并定期检查它们是否已被取消,以便它们可以停止执行。
然后,只需检查每个操作的
依赖关系
来检查这些依赖关系是否已取消。如果是,也只需退出/取消当前操作。Use an
NSOperationQueue
. Encapsulate each task in its ownNSOperation
. Set the dependencies usingaddDependency:
."How to prevent the other operations from being executed when one fails?", you ask.
Well, first, make sure your
NSOperation
objects are good citizens and check regularly whether they've been cancelled so they can stop execution.Then, just go through each operation's
dependencies
to check whether those dependencies are cancelled. If they are, just quit/cancel the current operation too.