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];
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.
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;
Related
I need to download multiple files in queue. Right now my code is working and all files are downloading simultaneously, However i need to download one file at a time, and all other files are in queue, below is code, please let me know what i doing wrong. I just need to download one file at a time and all other file should be in queue.
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:videoURL];
[httpClient.operationQueue setMaxConcurrentOperationCount:1];
for (NSURL *videoString in videoArray) {
NSURLRequest *request = [NSURLRequest requestWithURL:videoString];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if(operation.response.statusCode == 200) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Successfully Downloaded" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if(operation.response.statusCode!= 200) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Error While Downloaded" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}];
[httpClient enqueueHTTPRequestOperation:operation];
[operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
float percentDone = ((float)totalBytesRead) / totalBytesExpectedToReadForFile;
}
}];
To limit the queue to one Operation at a time,
you could try adding dependencies between each operation before you queue them.
Like this
[Operation2 addDependency:Operation1];
Hope that helps!
use dispatch queues(GCD) after the block setCompletionBlockWithSuccess:
like following way:
dispatch_async(dispatch_get_main_queue(), ^{
// Call any method from on the instance that created the operation here.
[self doSomework]; // example
});
When I am running my code, my code is stuck and gives log as:
void _WebThreadLockFromAnyThread(bool), 0x904db10: Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.
What is the reason, and what is the solution for this?
My Code:
-(IBAction)action:(id)sender{
NSLog(#"HI");
[self performSelectorInBackground:#selector(parse) withObject:nil];
}
-(void)parse{
rssOutputData = [[NSMutableArray alloc]init];
//[self performSelectorOnMainThread:#selector() withObject:nil waitUntilDone:NO];
[NSThread detachNewThreadSelector: #selector(startIndicator) toTarget: self withObject: self];
url=[[NSURL alloc]initWithString:[NSString stringWithFormat:#"http://service.technologyorg.com/service1/AuthenticateStudent=%#,%#,1",t1.text,t2.text]];
NSLog(#"A%#",url);
NSURLRequest *lRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
NSURLResponse *response;
NSError * error;
//[NSURLConnection connectionWithRequest:lRequest delegate:self];
[NSURLConnection sendSynchronousRequest:lRequest returningResponse:&response error:&error];
NSLog(#"AB%#",response);
if (nil == response) {
NSLog(#"%#",error);
[self performSelectorOnMainThread:#selector(updateTable) withObject:nil waitUntilDone:NO];
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:#"Connection failed!" message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
xmlData=[[NSMutableData alloc]initWithContentsOfURL:url];
NSLog(#"B%#",xmlData);
xmltitle=nil;
xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
[xmlParserObject setDelegate:self];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[xmlParserObject parse];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"XML:%#",xmllink);
if ([xmltitle isEqualToString:#"true"]) {
HomeViewController *lv=[[HomeViewController alloc]init];
lv.s1=[[NSString alloc]initWithFormat:#"%#",xmllink];
lv.s2=[[NSString alloc]initWithFormat:#"%#",xm1];
myStaticPassword=[[NSString alloc]initWithFormat:#"%#",xm2];
[self presentModalViewController:lv animated: YES];
[self performSelectorOnMainThread:#selector(updateTable) withObject:nil waitUntilDone:NO];
}else{
[self performSelectorOnMainThread:#selector(updateTable) withObject:nil waitUntilDone:NO];
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:#"Incorrect Username/Password" message:#"Please check your Username or Password" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil, nil];
[alert show];
}
});
});
}
I have an issue showing a UIAlertView on the main thread. I'm not sure why but it keeps crashing, despite me running on the main thread. The following block is on the background thread, but I have the alert on the main as below:
void (^removeFromCalendar)(NSString *, NSString *, EKEventStore *) = ^(NSString *error, NSString *eventKey, EKEventStore *eventDB) {
EKEvent *myEvent = [eventDB eventWithIdentifier:eventKey];
NSError *err = noErr;
if(myEvent != NULL && myEvent != (id)[NSNull null]) {
[eventDB removeEvent:myEvent span:EKSpanThisEvent error:&err];
} else {
// Event was not found, nothing to do
return;
}
[eventDB release];
if (!err || err == noErr) {
NSLog(#"Deleted event %#", myEvent.title);
// Show alert on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Showing the alert for unattending
NSString *resultString = #"This event was removed from your calendar.";
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Info" message:resultString delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil] autorelease];
[alert show];
});
return;
}
error = err.description;
};
If I comment out the bottom where it shows the alert, everything is fine. But for the alert, I keep getting a EXC_BAD_ACCESS error. Can somebody explain why? It's on the correct thread, and I cant for the life of me understand where the memory issue could come from!
May be you view is being released when you finish until you finish with the background queue. So, for safety why dont you use it like this;
...........
UIViewController __weak *myController = self;
dispathch_async(backgroundQueue, ^{
UIViewController __strong *myStrongController = myController;
...............
dispatch_async(dispatch_get_main_queue(), ^{
if(myStrongController){
// Showing the alert for unattending
NSString *resultString = #"This event was removed from your calendar.";
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Info" message:resultString delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil] autorelease];
[alert show];
}
});
}).
This is how you present an alert view:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"<#(NSString *)#>" message:#"<#(NSString *)#>" delegate:self cancelButtonTitle:#"<#(NSString *)#>" otherButtonTitles:nil];
[alert show];
[alert release];
Instead of using dispatch_async, why not use the objective C call:
[self performSelectorOnMainThread
You might have to package it up in its own method. Alternatively, call it using:
[self performSelector:#selector(myAlertMethod) withObject:nil afterDelay:0.25]
These methods have been tried and true since day 1.
I am calling URL on my Search button event to load data. I have put the code for Loading indicator Alert view by taking NSThread. I am using 3.2 xcode with 4.3 iOS. Every thing run smooth but on search button it shows Loading indicator and then crashing and showing following in console
Program received signal: “EXC_BAD_ACCESS”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.2 (8H7)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
Code under the Search Button click event:
- (IBAction) searchButton {
if([addressField.text length]==0)
{
UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Please Tap on 'Show Me' & choose the 'Radius' first!!!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] autorelease];
[myAlert show];
}
else
{
[NSThread detachNewThreadSelector:#selector(updateFilterProgress) toTarget:self withObject:nil];
appDelegate = (MapTutorialAppDelegate *)[[UIApplication sharedApplication] delegate];
CLLocationCoordinate2D location;
float radius = [[arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
NSString *url = [NSString stringWithFormat:#"http://....url...../hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];
NSLog(#"%#", url);
NSURL *URL = [NSURL URLWithString:url];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
{
if([appDelegate.markers count] == 0){
UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:#"Alert" message:#"No results fond!!!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] autorelease];
[myAlert show];
}
else
{
resultButton.userInteractionEnabled = YES;
for (int i = 0; i < [appDelegate.markers count]; i++)
{
marker *aMarker = [appDelegate.markers objectAtIndex:i];
location.latitude = [aMarker.lat floatValue];
location.longitude =[aMarker.lng floatValue];
AddressAnnotation *annob = [[AddressAnnotation alloc] initWithCoordinate:location];
annob.title = aMarker.name;
annob.subTitle = aMarker.address;
[mapView addAnnotation:annob];
[annob release];
CLLocationCoordinate2D ausLoc = {location.latitude,location.longitude}; //for zoom in the showroom results region
MKCoordinateSpan ausSpan = MKCoordinateSpanMake(0.108889, 0.169922);
MKCoordinateRegion ausRegion = MKCoordinateRegionMake(ausLoc, ausSpan);
NSLog(#"No Errors");
mapView.region = ausRegion;
}
}
}
else
NSLog(#"Error Error Error!!!");
[addressField resignFirstResponder];
}
}
And for NSThread to Show Loaading indicator while loading data at the back.
- (void) updateFilterProgress{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
Reachability *r = [Reachability reachabilityWithHostName:#"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:#"No Internet Connection" message:#"This app require an internet connection via WiFi or cellular network to work." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] autorelease];
[myAlert show];
}
else{
UIAlertView *alertMe = [[[UIAlertView alloc] initWithTitle:#"Loading..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alertMe show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(alertMe.bounds.size.width / 2, alertMe.bounds.size.height - 50);
[indicator startAnimating];
[alertMe addSubview:indicator];
[indicator release];
[alertMe release];
for (int i = 200; i > [appDelegate.markers count]; i--)
{
marker *aMarker = [appDelegate.markers objectAtIndex:i];
[alertMe dismissWithClickedButtonIndex:0 animated:YES];
}
}
[pool release]; }
Is there anything remaining in my code. Pleas correct me....
The variable alertMe is autoreleased and therefore you can't just send a release message to it. Remove the line [alertMe release]; and it will run perfectly.
You are probably accessing a released object. To see which one it is, set NSZombiesEnabled - this will show you which already released object you try to access, and you should be able to identify your problem.
Please have a look here to see how to enable the zombies in XCode 4:
http://42games.net/quick-note-on-setting-nszombieenabled-environment-variable-in-xcode-4/
I have the following code:
//View guest list
-(IBAction) guestList:(id) sender{
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"myURL"]];
//Waits a set peroid of time
wait(20000);
//Guest list is availible
if (connected != NULL){
CHARLIEAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate displayView:6];
}
//No network connection availible
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Network Connection!" message:#"Cannot establish internet connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
And I get the following warnings:
//Waits a set peroid of time
wait(20000);
Gives me -
warning: passing argument 1 of 'wait' makes pointer from integer without a cast
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://photostiubhart.comoj.com/testconnection.php"]];
Gives me -
warning: 'stringWithContentsOfURL:' is deprecated (declared at /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h:384)
I've done my testing and the code SEEMS to work fine, even though there are these warnings. Is there a way to remove these warnings at all? Or do they not matter?
I'm using xCode version 3.2.6
Thanks,
Jack
use
NSError* error;
NSString* connected = [NSString stringWithContentsOfURL:TheUrl encoding:NSASCIIStringEncoding error:&error];
You should use the method -
+(id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
for example -
NSString* text = [NSString stringWithContentsOfURL:TheUrl encoding:NSASCIIStringEncoding error:&error];
Hope it will help you.
Try doing it this way:
-(IBAction)guestList:(id) sender{
NSURL *requestUrl = [NSURL URLWithString:#"http://photostiubhart.comoj.com/testconnection.php"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl];
NSData *loadTest = [NSData dataWithContentsOfURL:requestUrl];
if (loadTest == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Network Connection!" message:#"Cannot establish internet connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
} else {
CHARLIEAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate displayView:6];
}
}