English 中文(简体)
发送问题——集团——没有在网络要求中产生两次反馈
原标题:Question about dispatch_group_t results in two callbacks in a network request

我使用<代码> 发送_group_t处理我的三项网络要求,其代码如下:

dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

[self.permissionData removeAllObjects];
dispatch_group_async(group, queue, ^{
    [self getOneData];
});

[self.bannerData removeAllObjects];
dispatch_group_async(group, queue, ^{
    [self getTwoData];
});

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    [self.tableView.mj_header endRefreshing];
});

and

- (void)getOneData {
    [self requestData:^(id  _Nullable datas, NSError * _Nullable error) {
          if (error != nil) {
              [self.tableView.mj_header endRefreshing];
          } else {
              if ([datas isKindOfClass:[NSArray class]]) {
                  self.datas = datas;
              }
              [self.tableView reloadData];
          }
    }];
}

这将造成两种方法的反馈:get OneDatagetTwoData两次。 我不知道他们为什么两次,但如果我不使用<条码>发送_group_t,则不存在问题。 我是否可以认为?

问题回答

由于你在你提交的各栏目中所做的工作本身是同步的,你可以依靠<条码>发送_group_async,为你管理集体进出。

页: 1 此前,在每项职能完成时,请打电话到<条码>发送_group_leave。

根据你目前的法典,小组在提交区块时即进入,而小组则在提交区块完成时离开。

由于requestData 是同步的,get OneData 回归,网络运行并不完备。

get OneData (和大概getTwoData)一样,从一个同步的组群中划出这种编号没有什么价值。

You also have a bug where both your completion handler for getOneData and your dispatch_group_notify call endRefreshing. This will result in an "unbalanced calls" exception if there is an error in the first data request.

基本想法是,你应当给你以按时代顺序完成手套:

- (void)getOneDataWithCompletion:(void (^)())completion {
    [self requestData:^(id _Nullable datas, NSError * _Nullable error) {
          …

          if (completion) {
              completion();
          }
    }];
}

和第二次APIC呼吁一样。

然后,在完成工作的人中,如:

dispatch_group_enter(group);
[self getOneDataWithCompletion:^{
    dispatch_group_leave(group);
}];

dispatch_group_enter(group);
[self getTwoDataWithCompletion:^{
    dispatch_group_leave(group);
}];

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    …
});

作为一项一般规则,如果你采用同步方法,则谨慎的做法是,在采用同步方法时,让你称之为“完成手脚”参数。 如果你认为没有必要这一参数,则将其编号为(因此,我检查一下,在我打电话之前该参数并不无效)。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签