1

I have a NetworkManager class which does the task of connecting to a JSON based web service and returning the server response. I am using a completion handler block to return the success or error response from NetworkManager (Internally, NetworkManager uses delegate based NSURLConnection).

NetworkManager *objNetworkManager= [[NetworkManager alloc]init];

[objNetworkManager setCompletionHandler:^(NSDictionary *resp, BOOL isSuccess){
    if (isSuccess) {

    }else{

    }
}];

[objNetworkManager initiateUrlRequestWithInput:jsonRequestInput];

Everything works fine now and my network requests work as expected using this NetworkManager class. My concern is that I am allocating an instance of NetworkManager each time I make an web service call. But, where should I set the objNetworkManager to nil? Because it uses a block callback, I'm not sure how to handle memory efficiently. Please advice!!

Rashmi Ranjan mallick
  • 6,390
  • 8
  • 42
  • 59

2 Answers2

2

Here is my take on this:

ARC automatically takes care of it for you if you are not creating reference cycles or any leaks. For instance, if VC1 instantiates NetworkManager and post everything is done, you go out of VC1 and it gets deallocated then NetworkManager reference will also be deallocated.

If you really want to be proactive to release NetworkManager then you can do it in your both success and error completion blocks. This is how I do it :).

EDIT: Example:

__weak MyViewController *aBlockSelf = self;

// Save User Preferences (/ics/markavailable)
self.requestHandler = [[MyRequestHandler alloc] initWithEndPoint:@"/fetch/request" body:aBodyData container:self.navigationController.view loadingOverlayTitle:@"Loading..." successHandler:^(NSDictionary *iResponse) {
    // Do success handling

    aBlockSelf.requestHandler = nil;
} andErrorHandler:^(NSString *iMessage, NSString *iKey, NSInteger iErrorCode, BOOL iIsNetworkError) {
   // Do success handling

   aBlockSelf.requestHandler = nil;
}];

[self.requestHandler executeRequest];

To understand retain cycle, take a look at this thread.

Community
  • 1
  • 1
Abhinav
  • 37,684
  • 43
  • 191
  • 309
0

If web service can be invoked at same time, you can use a NSMutableArray in a public data to save managers.

//Public data manager class add a NSMutableArray to save managers.
@property (nonatomic, strong) NSMutableArray *requestingManager;

NetworkManager *objNetworkManager= [[NetworkManager alloc]init];
[objNetworkManager setCompletionHandler:^(NSDictionary *resp, BOOL isSuccess){
    //remove when finish
    [[PublicData instance].requestingManager removeObject:objNetworkManager];
    if (isSuccess) {

    }else{

    }
}];
[objNetworkManager initiateUrlRequestWithInput:jsonRequestInput];
//add manager when request
[[PublicData instance].requestingManager addObject:objNetworkManager];

After remove objNetworkManager, objNetworkManager will not be retained, and will release, the block in it will be set to nil automatically.

simalone
  • 2,768
  • 1
  • 15
  • 20