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];
}

你可能感兴趣的:(iOS,iOS)