【移动应用开发技术】iOS如何实现当多个网络请求完成后执行下一步_第1页
【移动应用开发技术】iOS如何实现当多个网络请求完成后执行下一步_第2页
【移动应用开发技术】iOS如何实现当多个网络请求完成后执行下一步_第3页
【移动应用开发技术】iOS如何实现当多个网络请求完成后执行下一步_第4页
【移动应用开发技术】iOS如何实现当多个网络请求完成后执行下一步_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】iOS如何实现当多个网络请求完成后执行下一步

前言解决方法1.首先,我们创建一个项目,然后做一般性的做法,不做任何处理去连续请求一个接口10次://1.无处理

UIButton

*Btn1

=

[UIButton

buttonWithType:UIButtonTypeCustom];

Btn1.frame

=

CGRectMake(100,

100,

100,

40);

Btn1.backgroundColor

=

[UIColor

grayColor];

[Btn1

setTitle:@"noConduct"

forState:UIControlStateNormal];

[Btn1

addTarget:self

action:@selector(Btn1)

forControlEvents:UIControlEventTouchUpInside];

[self.view

addSubview:Btn1];//1.noConduct

-(void)Btn1{

NSString

*str

=

@"/p/6930f335adba";

NSURL

*url

=

[NSURL

URLWithString:str];

NSURLRequest

*request

=

[NSURLRequest

requestWithURL:url];

NSURLSession

*session

=

[NSURLSession

sharedSession];

for

(int

i=0;

i<10;

i++)

{

NSURLSessionDataTask

*task

=

[session

dataTaskWithRequest:request

completionHandler:^(NSData

*

_Nullable

data,

NSURLResponse

*

_Nullable

response,

NSError

*

_Nullable

error)

{

NSLog(@"%d%d",i,i);

}];

[task

resume];

}

NSLog(@"end");

}2017-12-04

17:10:10.503

DownImage[3289:261033]

end

2017-12-04

17:10:10.676

DownImage[3289:261080]

00

2017-12-04

17:10:10.704

DownImage[3289:261080]

11

2017-12-04

17:10:10.754

DownImage[3289:261096]

44

2017-12-04

17:10:10.760

DownImage[3289:261080]

22

2017-12-04

17:10:10.800

DownImage[3289:261096]

55

2017-12-04

17:10:10.840

DownImage[3289:261080]

77

2017-12-04

17:10:10.844

DownImage[3289:261082]

66

2017-12-04

17:10:10.846

DownImage[3289:261096]

33

2017-12-04

17:10:10.888

DownImage[3289:261096]

88

2017-12-04

17:10:10.945

DownImage[3289:261080]

992.使用GCD的dispatch_group_t//2.group

UIButton

*Btn2

=

[UIButton

buttonWithType:UIButtonTypeCustom];

Btn2.frame

=

CGRectMake(100,

200,

100,

40);

Btn2.backgroundColor

=

[UIColor

grayColor];

[Btn2

setTitle:@"group--"

forState:UIControlStateNormal];

[Btn2

addTarget:self

action:@selector(Btn2)

forControlEvents:UIControlEventTouchUpInside];

[self.view

addSubview:Btn2];//2.group--

-(void)Btn2{

NSString

*str

=

@"/p/6930f335adba";

NSURL

*url

=

[NSURL

URLWithString:str];

NSURLRequest

*request

=

[NSURLRequest

requestWithURL:url];

NSURLSession

*session

=

[NSURLSession

sharedSession];

dispatch_group_t

downloadGroup

=

dispatch_group_create();

for

(int

i=0;

i<10;

i++)

{

dispatch_group_enter(downloadGroup);

NSURLSessionDataTask

*task

=

[session

dataTaskWithRequest:request

completionHandler:^(NSData

*

_Nullable

data,

NSURLResponse

*

_Nullable

response,

NSError

*

_Nullable

error)

{

NSLog(@"%d%d",i,i);

dispatch_group_leave(downloadGroup);

}];

[task

resume];

}

dispatch_group_notify(downloadGroup,

dispatch_get_main_queue(),

^{

NSLog(@"end");

});

}2017-12-04

17:14:46.984

DownImage[3289:265374]

22

2017-12-04

17:14:46.987

DownImage[3289:265370]

11

2017-12-04

17:14:47.052

DownImage[3289:265383]

55

2017-12-04

17:14:47.065

DownImage[3289:265370]

44

2017-12-04

17:14:47.111

DownImage[3289:265379]

33

2017-12-04

17:14:47.121

DownImage[3289:265383]

66

2017-12-04

17:14:47.169

DownImage[3289:265383]

77

2017-12-04

17:14:47.192

DownImage[3289:265370]

99

2017-12-04

17:14:47.321

DownImage[3289:265383]

88

2017-12-04

17:14:47.747

DownImage[3289:265374]

00

2017-12-04

17:14:47.747

DownImage[3289:261033]

end2017-12-04

17:15:14.576

DownImage[3289:265942]

33

2017-12-04

17:15:14.626

DownImage[3289:265936]

22

2017-12-04

17:15:14.647

DownImage[3289:265944]

44

2017-12-04

17:15:14.648

DownImage[3289:265936]

00

2017-12-04

17:15:14.657

DownImage[3289:265943]

11

2017-12-04

17:15:14.709

DownImage[3289:265944]

55

2017-12-04

17:15:14.728

DownImage[3289:265944]

66

2017-12-04

17:15:14.734

DownImage[3289:265944]

77

2017-12-04

17:15:14.738

DownImage[3289:265943]

88

2017-12-04

17:15:14.816

DownImage[3289:265944]

99

2017-12-04

17:15:14.816

DownImage[3289:261033]

enddispatch_group_t

downloadGroup

=

dispatch_group_create();

dispatch_group_enter(downloadGroup);

dispatch_group_leave(downloadGroup);

dispatch_group_notify(downloadGroup,

dispatch_get_main_queue(),

^{

});3.使用GCD的信号量dispatch_semaphore_t//3.semaphore

UIButton

*Btn3

=

[UIButton

buttonWithType:UIButtonTypeCustom];

Btn3.frame

=

CGRectMake(100,

300,

100,

40);

Btn3.backgroundColor

=

[UIColor

grayColor];

[Btn3

setTitle:@"semaphore"

forState:UIControlStateNormal];

[Btn3

addTarget:self

action:@selector(Btn3)

forControlEvents:UIControlEventTouchUpInside];

[self.view

addSubview:Btn3];//3.semaphore--

-(void)Btn3{

NSString

*str

=

@"/p/6930f335adba";

NSURL

*url

=

[NSURL

URLWithString:str];

NSURLRequest

*request

=

[NSURLRequest

requestWithURL:url];

NSURLSession

*session

=

[NSURLSession

sharedSession];

dispatch_semaphore_t

sem

=

dispatch_semaphore_create(0);

for

(int

i=0;

i<10;

i++)

{

NSURLSessionDataTask

*task

=

[session

dataTaskWithRequest:request

completionHandler:^(NSData

*

_Nullable

data,

NSURLResponse

*

_Nullable

response,

NSError

*

_Nullable

error)

{

NSLog(@"%d%d",i,i);

count++;

if

(count==10)

{

dispatch_semaphore_signal(sem);

count

=

0;

}

}];

[task

resume];

}

dispatch_semaphore_wait(sem,

DISPATCH_TIME_FOREVER);

dispatch_async(dispatch_get_main_queue(),

^{

NSLog(@"end");

});

}2017-12-04

17:36:49.098

DownImage[3428:283651]

22

2017-12-04

17:36:49.144

DownImage[3428:284210]

00

2017-12-04

17:36:49.152

DownImage[3428:284213]

33

2017-12-04

17:36:49.158

DownImage[3428:283651]

11

2017-12-04

17:36:49.167

DownImage[3428:284210]

44

2017-12-04

17:36:49.235

DownImage[3428:284213]

88

2017-12-04

17:36:49.249

DownImage[3428:283651]

55

2017-12-04

17:36:49.252

DownImage[3428:283651]

77

2017-12-04

17:36:49.324

DownImage[3428:283651]

99

2017-12-04

17:36:49.468

DownImage[3428:284214]

66

2017-12-04

17:36:49.469

DownImage[3428:283554]

end2017-12-04

17:37:11.554

DownImage[3428:284747]

00

2017-12-04

17:37:11.555

DownImage[3428:284733]

11

2017-12-04

17:37:11.627

DownImage[3428:284748]

55

2017-12-04

17:37:11.661

DownImage[3428:284748]

22

2017-12-04

17:37:11.688

DownImage[3428:284747]

44

2017-12-04

17:37:11.709

DownImage[3428:284747]

66

2017-12-04

17:37:11.770

DownImage[3428:284733]

77

2017-12-04

17:37:11.774

DownImage[3428:284733]

88

2017-12-04

17:37:11.824

DownImage[3428:284747]

99

2017-12-04

17:37:11.899

DownImage[3428:284733]

33

2017-12-04

17:37:11.900

DownImage[3428:283554]

enddispatch_semaphore_t

sem

=

dispatch_semaphore_create(0);

dispatch_semaphore_signal(sem);

dispatch_semaphore_wait(sem,

DISPATCH_TIME_FOREVER);4.考虑新需求,10个网络请求顺序回调。//4.NSOperation

UIButton

*Btn4

=

[UIButton

buttonWithType:UIButtonTypeCustom];

Btn4.frame

=

CGRectMake(100,

400,

100,

40);

Btn4.backgroundColor

=

[UIColor

grayColor];

[Btn4

setTitle:@"NSOperation"

forState:UIControlStateNormal];

[Btn4

addTarget:self

action:@selector(Btn4)

forControlEvents:UIControlEventTouchUpInside];

[self.view

addSubview:Btn4];//4.NSOperation

-(void)Btn4{

NSString

*str

=

@"/p/6930f335adba";

NSURL

*url

=

[NSURL

URLWithString:str];

NSURLRequest

*request

=

[NSURLRequest

requestWithURL:url];

NSURLSession

*session

=

[NSURLSession

sharedSession];

NSMutableArray

*operationArr

=

[[NSMutableArray

alloc]init];

for

(int

i=0;

i<10;

i++)

{

NSBlockOperation

*operation

=

[NSBlockOperation

blockOperationWithBlock:^{

NSURLSessionDataTask

*task

=

[session

dataTaskWithRequest:request

completionHandler:^(NSData

*

_Nullable

data,

NSURLResponse

*

_Nullable

response,

NSError

*

_Nullable

error)

{

NSLog(@"%d%d",i,i);

}];

[task

resume];

//非网络请求

NSLog(@"noRequest-%d",i);

}];

[operationArr

addObject:operation];

if

(i>0)

{

NSBlockOperation

*operation1

=

operationArr[i-1];

NSBlockOperation

*operation2

=

operationArr[i];

[operation2

addDependency:operation1];

}

}

NSOperationQueue

*queue

=

[[NSOperationQueue

alloc]init];

[queue

addOperations:operationArr

waitUntilFinished:NO];

//YES会阻塞当前线程

#warning

-

绝对不要在应用主线程中等待一个Operation,只能在第二或次要线程中等待。阻塞主线程将导致应用无法响应用户事件,应用也将表现为无响应。

}2017-12-04

18:03:10.224

DownImage[3584:304363]

noRequest-0

2017-12-04

18:03:10.226

DownImage[3584:304362]

noRequest-1

2017-12-04

18:03:10.226

DownImage[3584:304363]

noRequest-2

2017-12-04

18:03:10.231

DownImage[3584:304363]

noRequest-3

2017-12-04

18:03:10.232

DownImage[3584:304362]

noRequest-4

2017-12-04

18:03:10.233

DownImage[3584:304362]

noRequest-5

2017-12-04

18:03:10.233

DownImage[3584:304363]

noRequest-6

2017-12-04

18:03:10.234

DownImage[3584:304363]

noRequest-7

2017-12-04

18:03:10.235

DownImage[3584:304363]

noRequest-8

2017-12-04

18:03:10.236

DownImage[3584:304363]

noRequest-9

2017-12-04

18:03:10.408

DownImage[3584:304597]

22

2017-12-04

18:03:10.408

DownImage[3584:304597]

00

2017-12-04

18:03:10.409

DownImage[3584:304597]

11

2017-12-04

18:03:10.461

DownImage[3584:304597]

55

2017-12-04

18:03:10.476

DownImage[3584:304363]

44

2017-12-04

18:03:10.477

DownImage[3584:304365]

66

2017-12-04

18:03:10.518

DownImage[3584:304365]

77

2017-12-04

18:03:10.537

DownImage[3584:304596]

88

2017-12-04

18:03:10.547

DownImage[3584:304362]

99

2017-12-04

18:03:11.837

DownImage[3584:304362]

332017-12-04

18:04:27.699

DownImage[3584:306401]

noRequest-0

2017-12-04

18:04:27.700

DownImage[3584:306405]

noRequest-1

2017-12-04

18:04:27.701

DownImage[3584:306401]

noRequest-2

2017-12-04

18:04:27.701

DownImage[3584:306405]

noRequest-3

2017-12-04

18:04:27.702

DownImage[3584:306401]

noRequest-4

2017-12-04

18:04:27.702

DownImage[3584:306405]

noRequest-5

2017-12-04

18:04:27.703

DownImage[3584:306401]

noRequest-6

2017-12-04

18:04:27.703

DownImage[3584:306401]

noRequest-7

2017-12-04

18:04:27.704

DownImage[3584:306401]

noRequest-8

2017-12-04

18:04:27.704

DownImage[3584:306401]

noRequest-9

2017-12-04

18:04:27.772

DownImage[3584:306397]

22

2017-12-04

18:04:27.779

DownImage[3584:306401]

00

2017-12-04

18:04:27.782

DownImage[3584:306409]

11

2017-12-04

18:04:27.800

DownImage[3584:306405]

33

2017-12-04

18:04:27.851

DownImage[3584:306401]

66

2017-12-04

18:04:27.855

DownImage[3584:306397]

55

2017-12-04

18:04:27.915

DownImage[3584:306397]

77

2017-12-04

18:04:27.951

DownImage[3584:306397]

99

2017-12-04

18:04:27.953

DownImage[3584:306405]

88

2017-12-04

18:04:28.476

DownImage[3584:306409]

445.还是使用信号量semaphore完成4的需求//5.semaphoreorder

UIButton

*Btn5

=

[UIButton

buttonWithType:UIButtonTypeCustom];

Btn5.frame

=

CGRectMake(100,

500,

100,

40);

Btn5.backgroundColor

=

[UIColor

grayColor];

[Btn5

setTitle:@"order"

forState:UIControlStateNormal];

[Btn5

addTarget:self

action:@selector(Btn5)

forControlEvents:UIControlEventTouchUpInside];

[self.view

addSubview:Btn5];//5.semaphore--order

-(void)Btn5{

NSString

*str

=

@"/p/6930f335adba";

NSURL

*url

=

[NSURL

URLWithString:str];

NSURLRequest

*request

=

[NSURLRequest

requestWithURL:url];

NSURLSession

*session

=

[NSURLSession

sharedSession];

dispatch_semaphore_t

sem

=

dispatch_semaphore_create(0);

for

(int

i=0;

i<10;

i++)

{

NSURLSessionDataTask

*task

=

[session

dataTaskWithRequest:request

completionHandler:^(NSData

*

_Nullable

data,

NSURLResponse

*

_Nullable

response,

NSError

*

_Nullable

error)

{

NSLog(@"%d%d",i,i);

dispatch_semaphore_signal(sem);

}];

[task

resume];

dispatch_semaphore_wait(sem,

DISPATCH_TIME_FOREVER);

}

dispatch_async(dispatch_get_main_queue(),

^{

NSLog(@"end");

});

}2017-12-05

10:17:28.175

DownImage[938:51296]

00

2017-12-05

10:17:28.331

DownImage[938:51289]

11

2017-12-05

10:17:28.506

DownImage[938:51289]

22

2017-12-05

10:17:28.563

DownImage[938:51289]

33

2017-12-05

10:17:28.662

DownImage[938:51289]

44

2017-12-05

10:17:28.733

DownImage[938:51296]

55

2017-12-05

10:17:28.792

DownImage[938:51296]

66

2017-12-05

10:17:28.856

DownImage[938:51286]

77

2017-12-05

10:17:29.574

DownImage[938:51289]

88

2017-12-05

10:17:29.652

DownImage[938:51286]

99

2017-12-05

10:17:29.653

DownImage[938:45252]

end2017-12-05

10:17:46.341

DownImage[938:51608]

00

2017-12-05

10:17:47.967

DownImage[938:51607]

11

2017-12-05

10:17:48.038

DownImage[938:51603]

22

2017-12-05

10:17:48.132

DownImage[938:51603]

33

2017-12-05

10:17:48.421

DownImage[938:51608]

44

2017-12-05

10:17:48.537

DownImage[938:51289]

5

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论