OGeek|极客世界-中国程序员成长平台

标题: ios - 在Objective-C中是否可以在一组其他方法执行完毕后调用一个方法 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:57
标题: ios - 在Objective-C中是否可以在一组其他方法执行完毕后调用一个方法

这可能是一个幼稚的问题,但在我加载 ViewController 时,我正在使用一组方法(如下面的 getEachItem)加载应用程序所需的所有内容。这通常是 2 或 3 个项目,它们都被写入缓存。

我想调用在 getEachItem 的最终实例完成后运行的方法“showNavigation”,但不确定如何执行此操作。 getEachItem 使用 AFNetworking 执行 GET 请求。类似于 jQuery 完整 block 的东西,但对于以下 for 循环的全部内容。

NSArray *tmpItems=[result objectForKey"ipad_items"];
for(NSDictionary *m in tmpItems){
  // will also increment into the _menus array
  [self getEachItem:[m objectForKey"id"]];
  [self getImages:[m objectForKey"id"]];
}
[self showNavigation];



Best Answer-推荐答案


当您调用 AFNetworking GET 方法时,它会返回一个 AFHTTPRequestOperation(一个 NSOperation 子类)。您可以利用这一事实为您的问题采用基于操作队列的解决方案。也就是说,您可以创建一个新的“完成操作”,这取决于特定 AFNetworking 操作的完成情况。

例如,您可以更改 getEachItem 方法以返回 GET 方法返回的 AFHTTPRequestOperation。例如,假设您有一个 getEachItem 当前定义如下:

- (void)getEachItemid)identifier
{
     // do a lot of stuff

     [self.manager GET:... parameters:... success:... failure:...];
}

将其更改为:

- (NSOperation *)getEachItemid)identifier
{
    // do a lot of stuff

    return [self.manager GET:... parameters:... success:... failure:...];
}

然后,您可以创建自己的完成操作,该操作将依赖于所有其他 AFHTTPRequestOperation 操作的完成。因此:

NSOperation *completion = [NSBlockOperation blockOperationWithBlock:^{
    [self showNavigation];
}];

NSArray *tmpItems=[result objectForKey"ipad_items"];
for(NSDictionary *m in tmpItems){
    // will also increment into the _menus array
    NSOperation *operation = [self getEachItem:[m objectForKey"id"]];
    [completion addDependencyperation];
    [self getImages:[m objectForKey"id"]];
}

[[NSOperationQueue mainQueue] addOperation:completion];

完成此操作后,在所有 getEachItem 操作完成之前,completion 操作将不会触发。请注意,当核心 AFHTTPRequestOperation 对象完成时,将触发此完成操作,但不能保证它们各自完成这些请求的 block 一定完成。


另一种方法是使用 GCD“组”。使用这种技术,您在提交每个请求时“进入”组,在 GET 方法的完成 block 中“离开”组。然后,您可以指定当群组通知您离开群组的次数与您进入群组的次数相同时要执行的代码块(即所有 AFNetworking 网络请求及其成功/failure block ,完成)。

例如,在 getEachItem 中添加一个 dispatch_group_t 参数:

- (void)getEachItemid)identifier groupdispatch_group_t)group
{
    dispatch_group_enter(group);

    // do a lot of stuff

    [self.manager GET:... parameters:... success:^(...) {
        // do you success stuff and when done, leave the group

        dispatch_group_leave(group);
    } failure:^(...) {
        // do you failure stuff and when done, leave the group

        dispatch_group_leave(group);
    }];
}

注意,您在提交请求之前“进入”组,successfailure block 都必须调用 dispatch_group_leave .

完成此操作后,您现在可以在请求循环中使用 dispatch_group_t,当组收到一切已完成的通知时执行 showNavigation:

dispatch_group_t group = dispatch_group_create();

NSArray *tmpItems=[result objectForKey"ipad_items"];
for(NSDictionary *m in tmpItems){
    // will also increment into the _menus array
    [self getEachItem:[m objectForKey"id"] group:group];
    [self getImages:[m objectForKey"id"]];
}

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    [self showNavigation];
});

关于ios - 在Objective-C中是否可以在一组其他方法执行完毕后调用一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465939/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4