简体   繁体   English

Objective-C中的Dispatch_sync或完成块差异

[英]Dispatch_sync or completion block difference in objective-c

I'm struggling with GCD and blocks. 我正在为GCD和块而苦苦挣扎。 I'm trying to create a series of methods that require data from a previous one. 我正在尝试创建一系列需要上一个数据的方法。 I was thinking about 2 different ways to achieve it. 我正在考虑两种不同的方法来实现它。

  1. dispatch_sync serial queue dispatch_sync序列queue列
  2. nested completion blocks 嵌套完成块

Don't you think the following 2 options return the same value? 您不认为以下两个选项返回相同的值吗? AS far as I read in Apple's dispatch queues , DISPATCH_QUEUE_SERIAL runs in FIFO order. 据我在Apple的调度队列中读取的信息, DISPATCH_QUEUE_SERIAL以FIFO顺序运行。 So both options should return identical values. 因此,两个选项应返回相同的值。

What am I doing wrong here? 我在这里做错了什么? and which one is the best approach? 最好的方法是哪一种? Thanks for your help! 谢谢你的帮助!

//Option 1

dispatch_queue_t delete_queue = dispatch_queue_create("delete_queue", DISPATCH_QUEUE_SERIAL);

dispatch_sync(delete_queue, ^{       
    [self dosomething];
});

dispatch_sync(delete_queue, ^{
    [self dosomething2];
});

dispatch_sync(delete_queue, ^{
    [self dosomething3]; 
});    

//Option 2

-(void)dosomething1:(dispatch_block_t)completion;
-(void)dosomething2:(dispatch_block_t)completion;
-(void)dosomething3:(dispatch_block_t)completion;

[self dosomething:^{ 
    [self dosomething2:^{
        [self dosomething3:^{}];
    }];
}];

-(void)dosomething:(dispatch_block_t)completion {

    /*method logic here*/
    completion();
}
-(void)dosomething2:(dispatch_block_t)completion {

   /*method logic here*/
   completion();
}
-(void)dosomething3:(dispatch_block_t)completion {

   /*method logic here*/
   completion();
}

Both code samples you have shown are equivalent to just: 您显示的两个代码示例仅等效于:

[self dosomething];
[self dosomething2];
[self dosomething3]; 

In other words, both ways execute the methods synchronously, in order, and block the thread until they are done. 换句话说,两种方式都按顺序同步执行方法,并阻塞线程直到完成。

Also, as Ken Thomases said, none of your methods "return" anything, so your question about returning doesn't make sense. 而且,正如Ken Thomases所说,您的方法都不“返回”任何东西,因此您关于返回的问题没有任何意义。

It doesn't really make sense to do three separate calls to dispatch_sync() here: 在这里对dispatch_sync()进行三个单独的调用实际上没有任何意义:

dispatch_sync(delete_queue, ^{       
    [self dosomething];
});

dispatch_sync(delete_queue, ^{
    [self dosomething2];
});

dispatch_sync(delete_queue, ^{
    [self dosomething3]; 
});    

You should instead just do them all in a single block: 相反,您应该将它们全部放在一个块中:

dispatch_sync(delete_queue, ^{       
    [self dosomething];
    [self dosomething2];
    [self dosomething3]; 
});    

As for the use of completion blocks, you can certainly obtain a similar result, except that the completion-handler result would need to be asynchronous. 至于完成块的使用,您当然可以获得类似的结果,只是完成处理程序的结果需要异步。

I think you need to take a step back and explain what kind of API you are trying to design in order to determine how you want to use the tools of the system and language to achieve that API design. 我认为您需要退后一步,并说明要尝试设计的API类型,以确定要如何使用系统工具和语言来实现该API设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM