iOS开发Google Protocol Buffer 的使用(三)

pragma mark - 获取数据

- (void)getData{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSString *subServerUrl = [defaults 	objectForKey:@"subURL"];//http://192.168.0.36:8181//api/Department
	//我以前做的时候传的是一个实体类参数NSData *data =[token req]就可以了,可是后来后台让我传两个实体,我想那也挺好做,用数据输出流转化成一个data就好了,但是没有成功(后台无法将这一个data转化成两个data)。这回可难住我了,怎么是好,百度无望,就是我这边想办法以分割的方式传data,我就大胆的在谷歌的第三方里加了一段代码,结果好使了。在最下面我给出了加的代码。
	//一个实体
	UToken *token = [[[[[UToken builder] setToken:[defaults 	objectForKey:@"token"]] setUid:[[defaults objectForKey:@"uid"] intValue]] setMethodName:@"DepartmentALL"] build];
//两个实体
	DepartmentListReq *req = [[[[DepartmentListReq builder] setDepId:0] setIsShowMember:YES] build];
	NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
	[rawOutput open];
	[token writeDelimitedToOutputStream:rawOutput];
	[req writeDelimitedToOutputStream:rawOutput];
	NSData* data = [rawOutput 	propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
	[PBHttpMethod postDataByString:[subServerUrl stringByAppendingString:@"Department"] BODYData:data WithDataBlock:^(id data) {
	DepartmentListRes *res = [DepartmentListRes parseFromData:data];
	_companyName = res.cmpInfo.name;
	[_userdefaults setObject:res.cmpInfo.name forKey:@"companyName"];
	//切记这里加上主线程要不UI界面无法更新(其实这也是个坑)
	dispatch_async(dispatch_get_main_queue(), ^{
	//这里我用了CoreData的本地化存储,用不到可以不刁它。
	NSFetchRequest *fetchRequest = [NSFetchRequest 	fetchRequestWithEntityName:@"Dep"];
	NSArray *allList = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
	_dataDepArray = [NSMutableArray arrayWithArray:allList];
	for (_dep in _dataDepArray) {
		[_manager.managedObjectContext deleteObject:_dep];
		[_manager saveContext];
	}
	NSFetchRequest *fetchRequest1 = [NSFetchRequest 	fetchRequestWithEntityName:@"Member"];
	NSArray *allList1 = [_manager.managedObjectContext executeFetchRequest:fetchRequest1 error:nil];
	for (_member in allList1) {
		[_manager.managedObjectContext deleteObject:_member];
		[_manager saveContext];
	}
	for (DepRow *deprow  in res.depList) {
		_dep = [NSEntityDescription 	insertNewObjectForEntityForName:@"Dep" inManagedObjectContext:_manager.managedObjectContext];
		_dep.iD = [NSNumber numberWithInt:deprow.id];
		_dep.name = deprow.name;
		_dep.pid = [NSNumber numberWithInt:deprow.pid];
		_dep.isChild = [NSNumber numberWithBool:deprow.isChild];
		_dep.memberCount = deprow.memberCount;
		[_manager saveContext];
		// NSLog(@"------%@", _dep.name);
	}
	for (MemberInfoRes *memres in res.memberList) {
		_member = [NSEntityDescription 		insertNewObjectForEntityForName:@"Member" 		inManagedObjectContext:_manager.managedObjectContext];
		_member.pid = memres.depId;
		_member.name = memres.name;
		_member.mobile = memres.mobile;
		_member.sex = memres.sex;
		_member.post = memres.post;
		_member.email = memres.email;
		_member.phone = memres.phone;
		_member.headPoto = memres.headPoto;
		_member.depName = memres.depName;
		_member.iD = memres.id;
		NSLog(@"->>>>><<<%@", memres.headPoto);
		[_manager saveContext];
	}
	[_userdefaults setObject:@"1" forKey:@"iSContract"];
	[self updateUI];
	[_tableView.mj_header endRefreshing];
	[_tableView.mj_footer endRefreshing];
	});
} WithResponseBlock:^(NSURLResponse *response) {
	NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
	NSInteger responseStatusCode = [httpResponse statusCode];
	if (responseStatusCode != 200) {
		dispatch_async(dispatch_get_main_queue(), ^{
			if (responseStatusCode == 0) {
				DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"提示" contentText:[NSString stringWithFormat:@"网络请求失败,请检查网络"] leftButtonTitle:nil rightButtonTitle:@"确定"];
				[alert show];
				[_tableView.mj_header endRefreshing];
				alert.leftBlock = ^() {
				NSLog(@"left button clicked");
				};
		}else{
				DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"提示" contentText:[NSString stringWithFormat:@"服务器异常,请稍后再试"] leftButtonTitle:nil rightButtonTitle:@"确定"];
				[alert show];
				[_tableView.mj_header endRefreshing];
				alert.leftBlock = ^() {
				NSLog(@"left button clicked");
			};
		}
	});
	}
} WithErroBlock:^(NSError *error) {
	if (error) {
		dispatch_async(dispatch_get_main_queue(), ^{
		DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"提示" contentText:[NSString stringWithFormat:@"解析失败,请稍后再试"] leftButtonTitle:nil rightButtonTitle:@"确定"];
		[alert show];
		[_tableView.mj_header endRefreshing];
		alert.leftBlock = ^() {
		NSLog(@"left button clicked");
		};
	});
	}
}];
}

我做的时候第三方里没有数据输出流分割传数据的方法,久思苦想在Protocol Buffer的
AbstractMessage.m文件里添加了如下方法后好使了:

- (void) writeToOutputStream:(NSOutputStream*) output {
	PBCodedOutputStream* codedOutput = [PBCodedOutputStream streamWithOutputStream:output];
	[self writeToCodedOutputStream:codedOutput];
	[codedOutput flush];
}
- (void) writeDelimitedToOutputStream:(NSOutputStream *)output
{
	int serialized = [self serializedSize];
	PBCodedOutputStream* codedOutput = [PBCodedOutputStream 			streamWithOutputStream:output];
	[codedOutput writeRawVarint32:serialized];
	[self writeToCodedOutputStream:codedOutput];
	[codedOutput flush];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allen.Su

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值