zoukankan      html  css  js  c++  java
  • For循环执行AFNetworking请求

    屏幕快照 2017-12-19 下午1.46.25.png

    需求:如下操作打印的文档为

    NSLog(@"开始");for(NSIntegeri =0; i <5; i++) {        [RequestUtil requestFavoriteGroup:appDelegate.userInfo.uid andBlock:^(NSArray*modelArr,NSError*error) {NSLog(@"第一层的循环请求,i=%ld",i);for(NSIntegerj =0; j <3; j++) {                [RequestUtil requestFavoriteGroup:appDelegate.userInfo.uid andBlock:^(NSArray*modelArr,NSError*error) {NSLog(@"第二层的循环请求,i=%ld,j=%ld",i,j);                }];            }        }];    }NSLog(@"结束");

    开始

    第一层的循环请求,i=0

    第二层的循环请求,i=0,j=0

    第二层的循环请求,i=0,j=1

    第二层的循环请求,i=0,j=2

    第一层的循环请求,i=1

    第二层的循环请求,i=1,j=0

    第二层的循环请求,i=1,j=1

    第二层的循环请求,i=1,j=2

    第一层的循环请求,i=2

    第二层的循环请求,i=2,j=0

    第二层的循环请求,i=2,j=1

    第二层的循环请求,i=2,j=2

    第一层的循环请求,i=3

    第二层的循环请求,i=3,j=0

    第二层的循环请求,i=3,j=1

    第二层的循环请求,i=3,j=2

    第一层的循环请求,i=4

    第二层的循环请求,i=4,j=0

    第二层的循环请求,i=4,j=1

    第二层的循环请求,i=4,j=2

    结束

    一种写法:

    dispatch_async(dispatch_get_global_queue(0,0), ^{NSLog(@"开始");        dispatch_semaphore_t sema = dispatch_semaphore_create(0);for(NSIntegeri =0; i <5; i++) {            [RequestUtil requestFavoriteGroup:@"8130d4174c2353af"andBlock:^(NSArray*modelArr,NSError*error) {NSLog(@"第一层的循环请求,i=%ld",i);dispatch_async(dispatch_get_global_queue(0,0), ^{                    dispatch_semaphore_t sema2 = dispatch_semaphore_create(0);for(NSIntegerj =0; j <3; j++) {                        [RequestUtil requestFavoriteGroup:appDelegate.userInfo.uid andBlock:^(NSArray*modelArr,NSError*error) {NSLog(@"第二层的循环请求,i=%ld,j=%ld",i,j);                            dispatch_semaphore_signal(sema2);                        }];                        dispatch_semaphore_wait(sema2, DISPATCH_TIME_FOREVER);                    }                    dispatch_semaphore_signal(sema);                });                            }];            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);        }NSLog(@"结束");    });

  • 相关阅读:
    s111 stark组件
    数据结构
    django基础
    15个值得开发人员关注的jQuery开发技巧和心得
    关于浏览器事件的思考
    关于浏览器事件的思考
    浅入javascript正则表达式的规则.
    JQuery常用功能的性能优化
    vim
    常用免费的WebService列表
  • 原文地址:https://www.cnblogs.com/striveLD/p/9953734.html
Copyright © 2011-2022 走看看