这个过程大致是这个样子:
1 添加通知
2 发送网络请求 里边有一个发送通知的操作
3 执行发送通知的具体操作
代码如下:
1 在VC添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivingYM1:) name:@"YM1" object:nil];
2 在SYNDispatchProcessor.m 中进行发送通知
-(void) delayTimeGCDbuild { // NSLog(@"主线程 %@", [NSThread currentThread]); //间隔还是1秒 uint64_t interval = 1 * NSEC_PER_SEC / 100 ; //创建一个专门执行timer回调的GCD队列 // dispatch_queue_t queue = dispatch_queue_create("my queue", 0); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //创建Timer _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); //使用dispatch_source_set_timer函数设置timer参数 dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0); //设置回调 dispatch_source_set_event_handler(_timer, ^() { [self.arrayLock lock]; if([protocolmutableArrays count]>0) { NSData* nData = [protocolmutableArrays objectAtIndex:0]; // NSLog(@"Hava received datas is----- :%@",[self nDataToGbkString:nData]); NSData* d = [nData subdataWithRange:NSMakeRange(5, 3)]; NSString* aStr = [[NSString alloc] initWithData:d encoding:NSASCIIStringEncoding]; NSLog(@"在这里进行类型的判断:%@",aStr); { //在这里发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:aStr object:[nData subdataWithRange:NSMakeRange(10, nData.length-10)]]; } [protocolmutableArrays removeObjectAtIndex:0]; } [self.arrayLock unlock]; });
3 执行发送通知的事件
//发送完通知之后接收到漫画数据 - (void) receivingYM1:(NSNotification*) anotification { //收到的是漫画的4个cell的大图跟一些相关信息 NSData* nsData = [anotification object]; NSString *string = [[NSString alloc]initWithData:nsData encoding:NSUTF8StringEncoding]; NSLog(@"nsdata:%@",string); [self performSelectorOnMainThread:@selector(updateResgContollerYM1:) withObject:nsData waitUntilDone:YES]; } //更新幽默坊漫画ui -(void) updateResgContollerYM1:(id) data { //将data转换成字典类型 NSDictionary* YM1Dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; //用数组进行接收 NSMutableArray *commicArr = [YM1Dict valueForKey:@"comicList"]; //将数据存到字典中 self.FirComicDic = [commicArr objectAtIndex:0]; self.SndComicDic = [commicArr objectAtIndex:1]; self.ThdComicDic = [commicArr objectAtIndex:2]; self.FourthComicDic = [@{@"comicContent":@"酒鬼",@"comicId":@"4"} mutableCopy]; }