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

ios - NSBlockOperation 在执行之前不等待依赖

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

我正在学习 NSOperationsNSOperationQueue

我有一组 NSBlockOperation:“上传”和“DELETE”。删除必须等待上传完成后再执行。

我希望在进行下一组操作之前完成一组操作。

我已经使用 NSThread sleepForTimeInterval 来模拟上传等待和删除延迟时间。

但是这些操作并没有等待集合完成。

我将 maxConcurrentOperationCount 设置为 1,但这似乎不起作用。

您可以从输出中看到 Set 1 完成得很好。

但是在 Delete 2 完成之前,第二组 Upload 3 就盯着看。然后上传/删除 4 就可以了。然后从那里开始变得更加困惑。

有什么帮助吗?

输出:

Start UPLOAD 1
Completed UPLOAD 1
Start DELETE 1
Completed DELETE 1

Start UPLOAD 2
Completed UPLOAD 2
Start DELETE 2
Start UPLOAD 3
Completed DELETE 2
Start DELETE 3
Completed UPLOAD 3
Completed DELETE 3

Start UPLOAD 4
Start DELETE 4
Completed UPLOAD 4
Completed DELETE 4

Start UPLOAD 5
Start DELETE 5
Completed UPLOAD 5
Start UPLOAD 6
Completed DELETE 5
Start DELETE 6
Completed UPLOAD 6
Start UPLOAD 7
Completed DELETE 6

代码:

- (void)viewDidLoad {
    [super viewDidLoad];


    NSOperationQueue *operationQueue = [NSOperationQueue mainQueue];
    operationQueue.maxConcurrentOperationCount = 1;


//pretend there are 100 images that need to be uploaded and information in a SQLite DB waiting to be deleted upon successful upload of the image.

//Upload 1 image at a time, upon successful upload, delete the respective DB info then move to the next image 
    for (__block int i = 1; i < 100; i++){
        NSOperation * UPLOAD = [self createNewOperationWithInt:i Message"UPLOAD"];
        NSOperation * DELETE = [self createNewOperationWithInt:i Message"DELETE"];
        [DELETE addDependency:UPLOAD];
        [operationQueue addOperation:UPLOAD];
        [operationQueue addOperationELETE];

    }

}

- (NSBlockOperation *) createNewOperationWithIntint)i MessageNSString*)message {
    NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"Start %@ %i",message , i);

        if ([message containsString"UPLOAD"]) {
             [NSThread sleepForTimeInterval:1]; //Pretend there is Network latency on upload
        }

        if ([message containsString"DELETE"]) {
            [NSThread sleepForTimeInterval:0.5]; //Pretend the SQLDB is being accessed
        }

    }];
    operation.queuePriority = NSOperationQueuePriorityNormal;
    operation.qualityOfService  = NSOperationQualityOfServiceUtility;
    operation.completionBlock = ^{
       NSLog(@"Completed %@ %i",message , i);
    };

    return operation;
}



Best Answer-推荐答案


这种行为似乎很好。 “问题”是完成 block 和依赖项都在 upload 任务完成后运行。这就是为什么您有时会在“完成上传 N”之前得到“开始删除 N+1”。

尝试在 block 操作结束时添加一条消息,以检查它是否在开始下一个操作之前完成:

NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"Start %@ %i",message , i);

    if ([message containsString"UPLOAD"]) {
         [NSThread sleepForTimeInterval:1]; //Pretend there is Network latency on upload
    }

    if ([message containsString"DELETE"]) {
        [NSThread sleepForTimeInterval:0.5]; //Pretend the SQLDB is being accessed
    }

    NSLog(@"Finished %@ %i",message , i);
}];

关于ios - NSBlockOperation 在执行之前不等待依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43545417/

回复

使用道具 举报

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

本版积分规则

关注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