views:

630

answers:

2

Hello all,

I am using a private MBProgressHUD

Now I am using the indicator view on my add button in which I am calling my addrecord service .

UIWindow *window = [UIApplication sharedApplication].keyWindow;
 HUD = [[MBProgressHUD alloc] initWithWindow:window];

 // Add HUD to screen
 [window addSubview:HUD];

 // Regisete for HUD callbacks so we can remove it from the window at the right time
 HUD.delegate = self;

 // Show the HUD while the provided method executes in a new thread
 [HUD showWhileExecuting:@selector(addingToFavorites) onTarget:self withObject:nil animated:YES];

the adding to favorites method :

NSURL *url = [NSURL URLWithString:urlstring];

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
     [request setHTTPMethod:@"GET"];
     //[request setTimeoutInterval:10];
     //NSURLResponse *response = nil;
//   NSError *error = nil;
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
     NSData *data1= [NSURLConnection sendSynchronousRequest:request          
               returningResponse:nil error:nil];

     if(data1 == nil)
     {
      doneFlag = NO;
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert"
                    message:@"The network is not available.\n Please check the Internet connection."
                   delegate:nil
                cancelButtonTitle:@"OK"
                otherButtonTitles:nil];
      [alert show];
      [alert release];

     }
     else
     {
      doneFlag = YES;



      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Confirmation"
                    message:@"Added To favorites" 
                   delegate:nil
                cancelButtonTitle:@"OKAY"
                otherButtonTitles:nil];
      [alert show];
      alert = nil;
      [alert release];




     }


     [request release];

This is all running fine except the instruments gives leak of the uialertview may be it is conflicting with the mbprogreshud.

So I thought to remove the alert from the calling method and put it in the caller the method like this:

the caller method now :

UIWindow *window = [UIApplication sharedApplication].keyWindow;
     HUD = [[MBProgressHUD alloc] initWithWindow:window];

     // Add HUD to screen
     [window addSubview:HUD];

     // Regisete for HUD callbacks so we can remove it from the window at the right time
     HUD.delegate = self;

     // Show the HUD while the provided method executes in a new thread
     [HUD showWhileExecuting:@selector(addingToFavorites) onTarget:self withObject:nil animated:YES];
     //it should wait for the above line to be executing   ******* then to exexute the be //below condition but how ?


     if(doneFlag == NO){
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert"
                    message:@"The network is not available.\n Please check the Internet connection."
                   delegate:nil
                cancelButtonTitle:@"OK"
                otherButtonTitles:nil];
      [alert show];
      [alert release];
     }else{
      [favoritesButton setTitle:@"Remove" forState:UIControlStateNormal];
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Confirmation"
                    message:@"Added To favorites" 
                   delegate:nil
                cancelButtonTitle:@"OKAY"
                otherButtonTitles:nil];
      [alert show];
      alert = nil;
      [alert release];

     }

the adding to favorites method :

NSURL *url = [NSURL URLWithString:urlstring];

         NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
         [request setHTTPMethod:@"GET"];
         //[request setTimeoutInterval:10];
         //NSURLResponse *response = nil;
    //   NSError *error = nil;
        [[NSURLCache sharedURLCache] setMemoryCapacity:0];
        [[NSURLCache sharedURLCache] setDiskCapacity:0];
         NSData *data1= [NSURLConnection sendSynchronousRequest:request          
                   returningResponse:nil error:nil];

         if(data1 == nil)
         {
          doneFlag = NO;


         }
         else
         {
          doneFlag = YES;


         }


         [request release];

In the launching of the progresshud thread is detaching something like this :

[NSThread detachNewThreadSelector:@selector(launchExecution) toTarget:self withObject:nil]

Now My question is that If I follow the first scenario . How can I assure the the alertview leak will not come

Or If I am following the second scenario How can I assure the if condition will be executed after completing this line executed :

[HUD showWhileExecuting:@selector(addingToFavorites) onTarget:self withObject:nil animated:YES];
+1  A: 

Regarding the first scenario, it is in general a bad idea to do UI updates from threads other than the applications main thread. UIKit is NOT thread safe and doing threaded UI updates can cause all sorts of strange things to happen. Now, I'm not sure if this is the cause for the leak but I would avoid showing an UIAlertView in addingToFavorites. Use performSelectorOnMainThread or the second scenario described below.

Regarding the second scenario, move everything below the showWhileExecuting call to the hudWasHidden delegate method. At this point you can be sure that your code was fully executed and the doneFlag was set.

To use performSelectorOnMainThread, define a new method, put your code in it and than call performSelectorOnMainThread.

I.e.,

- (void)showAlert {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

Call with,

[self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];

I would go with the second scenario though.

Matej Bukovinski
can you explain how to use performSelectorOnMainThread ?
hib
I have done with the second scenario putting the code in the hudWasHidden
hib
Its working fine with the second scenario .
hib
+1  A: 

Other answers notwithstanding, you were creating the UIAlertView leak with this sequence:

[alert show];
alert = nil;
[alert release];

The last two lines should be swapped:

[alert show];
[alert release];
alert = nil;
wkw
not working as the matej said it may have conflict of uikit with the thread running . There is a method in the MBProgressHUD in that a thread is creating and it conflicts with whatever uikit things i do int that method .
hib