在 iPhone 上设计和启动冗长、依赖的异步进程的最佳方法是什么?

发布于 2024-10-04 21:46:50 字数 1129 浏览 8 评论 0原文

我正在设计一款 iPhone 应用程序。我有一个非常紧张的过程要开始,在高层定义如下:

  1. 用户从 UITableView 中为 inApp 选择项目 购买。
  2. 用户在 UI 中确认购买。
  3. 异步进程去App Store检查是否有item 之前已经购买过。如果不, 该物品已购买。 iPhone 然后通知应用程序该项目 购买成功。 (如果是 之前购买的应用程序 不关心并继续执行步骤 4。)
  4. 然后应用程序会转到我的私人服务器下载所购买商品的数据。
  5. 然后应用程序将数据导入 CoreData。

当然,在该过程的每个步骤中,都会向用户报告错误,并且整个过程会停止。

我有一个名为 ActivityIndi​​catorController 的类,它为用户提供了一个很好的 UIActivityIndi​​cator 以及一些指示正在发生的情况的文本。在该过程的每个步骤中,activityIndi​​cator 上的标签必须更新为新状态。

问题/担忧:

从设计和编码的角度来看,最简单的事情似乎是对所有步骤使用一个大的 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:

  1. User selects item from UITableView for inApp
    purchase.
  2. User confirms purchase in UI.
  3. 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.)
  4. The app then goes to my private server to download the data for the item purchased.
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

写下不归期 2024-10-11 21:46:50

通过将每个步骤添加到 NSOperationQueue 中,您可以在继续之前进行检查以确保该步骤失败。请参阅这篇关于一般方法的文章

如果操作遇到问题,您还可以发起并响应来自 NSOperation 的取消请求。请参阅此 响应取消事件 “nofollow”>文章

响应取消事件:

操作开始执行后,
它继续执行其任务,直到
它已完成或直到您的代码
显式取消操作。
取消可以随时发生,
甚至在手术开始之前
执行。虽然 NSOperation
课程为客户提供了一种方法
取消操作,识别
取消活动是自愿的
必要性。如果一个手术是
彻底终止,可能不会
是一种恢复记忆或
已分配的资源。作为
结果,操作对象是
预计会检查取消情况
事件并优雅退出
它们发生在中间
操作。

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:

After an operation begins executing,
it continues performing its task until
it is finished or until your code
explicitly cancels the operation.
Cancellation can occur at any time,
even before an operation begins
executing. Although the NSOperation
class provides a way for clients to
cancel an operation, recognizing the
cancellation event is voluntary by
necessity. If an operation were
terminated outright, there might not
be a way to reclaim any memory or
resources that had been allocated. As
a result, operation objects are
expected to check for cancellation
events and to exit gracefully when
they occur in the middle of the
operation.

燃情 2024-10-11 21:46:50

使用 NSOperationQueue。将每个任务封装在其自己的 NSOperation 中。使用addDependency: 设置依赖项。

“当一个操作失败时,如何防止执行其他操作?”,您问。

好吧,首先,确保您的 NSOperation 对象是好公民,并定期检查它们是否已被取消,以便它们可以停止执行。

然后,只需检查每个操作的依赖关系来检查这些依赖关系是否已取消。如果是,也只需退出/取消当前操作。

Use an NSOperationQueue. Encapsulate each task in its own NSOperation. Set the dependencies using addDependency:.

"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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文