• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 并发 NSOperationQueue 上传,后跟单个任务

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:26:54 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试执行两步流程:

  1. 同时上传照片
  2. 用户输入然后发布内容

,这与“添加照片”时 facebook 应用程序的工作方式非常相似

我想先在后台使用默认的 NSOperationQueueDefaultMaxConcurrentOperationCount 开始上传照片。这些操作将返回一些我需要与第二个帖子一起发送的信息。

但是,由于应用程序的性质和并发性,我如何才能以仅在满足以下条件时才发布内容的方式构建它:

  1. 所有照片均已上传
  2. 用户发布内容的操作

可能会发生几种情况:

  1. 无照片可发,用户发内容,内容应立即发布
  2. 照片上传完成,用户发布内容,内容应立即发布
  3. 照片上传完成,用户发布内容,需要等待所有照片上传完成才能发布内容。

我想我应该创建一个照片上传 NSOperationQueue 并检查 operationCount。如果是== 0,内容应该在用户点击UIButton时立即发布。

但是,如果照片上传未完成,我该如何等待?我是否应该在伪代码中以下列方式使用 waitUntilAllOperationsAreFinished:

// User taps on UIButton to post his content and this is in a ViewController
- void postMyContent:
{
   [self.photoUploadQ.waitUntilAllOperationsAreFinished]

   // Post my content now              
}

这是正确的吗?我无法将第二个任务添加到 photoUploadQ,因为它具有默认并发性。

由于文档,我担心死锁/阻塞:

When called, this method blocks the current thread and waits for the receiver’s current and queued operations to finish executing. While the current thread is blocked, the receiver continues to launch already queued operations and monitor those that are executing. During this time, the current thread cannot add operations to the queue, but other threads may. Once all of the pending operations are finished, this method returns.

If there are no operations in the queue, this method returns immediately.

谢谢!



Best Answer-推荐答案


一种常见的操作队列模式是创建一个操作(在您的情况下为“发布内容”操作),该操作依赖于一系列其他操作(在您的情况下为“图像上传”操作)的完成,使用NSOperation 方法,addDependency .这样,虽然队列可能是并发的,但在其他“图片上传”操作完成之前,该“发布内容”操作不会开始。

您可能还希望将您的操作队列限制为小于 NSOperationQueueDefaultMaxConcurrentOperationCount 因为 NSURLConnection 通常只允许 5 个左右的并发操作(如果您做的不止这些) ,其中一些最终可能会超时)。

关于ios - 并发 NSOperationQueue 上传,后跟单个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18733247/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap