[objective-c] Add some code to block method for decrementActivityCount with AFNetwork

AFNetwork 라이브러리를 사용하는데 네트워크 인디케이터 뺑뺑이를 돌리기를 멈추는 코드를 매번 써줘야해서 귀찮았다.

- (void) didLoadJson:(NSString *)json
{
	NSLog(@"\n%@", json);
}

- (IBAction)didClick:(id)sender
{
	__weak id weakSelf = self;
	[self requestWithUrl:@"http://www.redmine.org/projects.json" andBlock:^(AFHTTPRequestOperation *operation, id responseObject) 
	{
		[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];  // <- here
		[weakSelf didLoadJson:responseObject];
	}];
}

- (IBAction)didClick2:(id)sender
{
	__weak id weakSelf = self;
	[self requestWithUrl:@"http://www.redmine.org/issues/16036.json" andBlock:^(AFHTTPRequestOperation *operation, id responseObject)
	{
		[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];  // <- here
		[weakSelf didLoadJson:responseObject];
	}];
}

-(void) requestWithUrl:(NSString*)url andBlock:(void(^)(AFHTTPRequestOperation *operation, id responseObject))block
{
	[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];

	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:url
	  parameters:nil
		 success:block
		 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
		NSLog(@"Error: %@", error);
		[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
	}];
}

 

매 블럭마다 이 한 줄이 추가돼야 하는게 너무 마음에 안들었다.

[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];

 

옆자리 직원의 도움을 받아서

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/Blocks/Articles/bxDeclaringCreating.html

이 링크를 받았고, 이 중에 Creating a Block 부분을 참조했다.

float (^oneFrom)(float);

oneFrom = ^(float aFloat) {
    float result = aFloat - 1.0;
    return result;
};

 

그래서 이렇게 바꿨다.

- (void) didLoadJson:(NSString *)json
{
	NSLog(@"\n%@", json);
}

- (IBAction)didClick:(id)sender
{
	__weak id weakSelf = self;
	[self requestWithUrl:@"http://www.redmine.org/projects.json" andBlock:^(AFHTTPRequestOperation *operation, id responseObject) 
	{
		[weakSelf didLoadJson:responseObject];
	}];
}

- (IBAction)didClick2:(id)sender
{
	__weak id weakSelf = self;
	[self requestWithUrl:@"http://www.redmine.org/issues/16036.json" andBlock:^(AFHTTPRequestOperation *operation, id responseObject)
	{
		[weakSelf didLoadJson:responseObject];
	}];
}

-(void) requestWithUrl:(NSString*)url andBlock:(void(^)(AFHTTPRequestOperation *operation, id responseObject))block
{
	[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];

	void (^newBlock)(AFHTTPRequestOperation *operation, id responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject)
	{
		[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
		block(operation, responseObject);
	};

	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:url
	  parameters:nil
		 success:newBlock
		 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
		NSLog(@"Error: %@", error);
		[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
	}];
}