iPhone/Objective-C: How to modify UILabel from timer - iphone

I have the following timer:
uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(uploadGPS_tick:) userInfo:nil repeats:YES];
[self uploadGPS_tick:nil];
And here is the callback: uploadGPS_tick():
-(void)uploadGPS_tick:(NSTimer*)timer{
if(!lat || !lng){
//do nothing
}else{
NSString *urlStr=[[NSString alloc] initWithFormat:#"http://www.example.com/ajax/updateCoords.php"];
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
[request setPostValue:lat forKey:#"lat"];
[request setPostValue:lng forKey:#"lng"];
NSLog(#"EOH: %#",lat);
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *response=[request responseString];
NSLog(#"%#",response);
if([response isEqualToString:#"LO"]){
[self.navigationItem setBackBarButtonItem:nil];
DriverLogin *x= [[DriverLogin alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:x animated:NO];
}
SBJsonParser *parser=[[SBJsonParser alloc]init];
NSMutableDictionary *obj=[[NSMutableDictionary alloc]init ];
obj=[[parser objectWithString:[request responseString] error:nil]retain];
credits.text=[[obj objectForKey:#"credits"] stringValue]; //this won't show...
creditsUsed.text=[[obj objectForKey:#"creditsUsed"] stringValue]; //this won't show...
NSInteger timeLeftSecs=[[obj objectForKey:#"creditTimeLeft"] intValue];
NSInteger timeLeftMins=(int)(timeLeftSecs/60);
creditTimeLeft.text=[[NSString alloc]initWithFormat:#"%d",timeLeftMins]; //this won't show...
NSLog(#"xxx:%#",obj);
}];
[request setFailedBlock:^{
NSError *error =[request error];
NSLog(#"%#",error);
//do nothing
}];
[request startAsynchronous];
}
}
As you can see, every five seconds, a JSON object is sent from the server. This JSON object is then parsed and three UILabels are set based on this JSON data.
The trouble I'm having is that the UILabels aren't getting their text set! Even though I can clearly see NSLog(#"xxx:%#",obj); in the debugger. The UILabels are connected properly in the .xib.
Any help greatly appreciated.

You should do it from main thread. Replace the label text assignment with following code:
dispatch_async(dispatch_get_main_queue(), ^{
creditTimeLeft.text=[NSString stringWithFormat:#"%d",timeLeftMins];
});

Related

UIAlertView is showing up late

I am communicating with server in my ios app. I have following method in which I'm opening an alertview. I want to show a loading view while app is getting response from the server.
- (void) showDetailedQuestion:(id)sender
{
//loading view
self.loading_alert = [[UIAlertView alloc] initWithTitle:#"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[self.loading_alert 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(loading_alert.bounds.size.width / 2, loading_alert.bounds.size.height - 50);
[indicator startAnimating];
[self.loading_alert addSubview:indicator];
UIButton *btn = (UIButton*)sender;
int indx = btn.tag;
NSLog(#"tag:%d",indx);
answerAnQuestion *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Answer"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal ;
vc.que_id = [self.que_id valueForKey:[NSString stringWithFormat:#"%d",indx]];
vc.qid_list = self.que_id;
vc.crnt = indx;
[self presentViewController:vc animated:YES completion:nil];
[self.loading_alert dismissWithClickedButtonIndex:0 animated:YES];
}
and in another answerAnQuestion.m
- (void)viewDidLoad
{
NSString *address = [NSString stringWithFormat:#"%#%#%#%#%#%#%#", path,#"questions/",que_id,#"?token=",token,#"&user_id=",usrId];
NSURL *URL = [NSURL URLWithString:address];
NSLog(#"%#",address);
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
responseData = [[NSMutableData alloc] init];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (data)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
//If you need the response, you can use it here
int statuscode = [httpResponse statusCode];
NSString *responseMsg = [NSHTTPURLResponse localizedStringForStatusCode:statuscode];
NSLog(#" Status code: %d",statuscode );
NSLog(#" Status msg: %#",responseMsg );
}
else
{
// Handle error by looking at response and/or error values
NSLog(#"%#",error);
}
}
My problem is alertview is only shown up for a moment when view is changing. It suppose to open when I click the button. What could be the reason? how to solve this?
EDIT 1:
If i make asynchronous request to server then i'm not able to set those data in my tableview. I can set those data in my tableview Only if send synchronous request,but it blocks the app. Why this is happening ?
Any help will be appreciated.
Thank you.
You are sending SynchronousRequest on main thread, so it is blocking your UI thread. Read multithreading you will get various tutorial on this. I can suggest you to go for GCD or NSOperation and NSOperationQueue. Google for any of the above and you will get various sample for the same.
Or you can send asynchronous request as follows...
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//Write code you want to call when data is received,
//Like dismissing loading view and populating UI.
}];
Updated:
//Display alert view, before sending your request..
[alertview show];
//send first request
[NSURLConnection sendAsynchronousRequest:request1 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//Write code you want to call when data is received,
//send second request
[NSURLConnection sendAsynchronousRequest:request2 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//Write code you want to call when data is received,
//send third request
[NSURLConnection sendAsynchronousRequest:request3 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//Write code you want to call when data is received,
//dismiss alert view on main thread
dispatch_async(getmainqueue, ^(void) {
// dismiss alert view...
});
}];
}];
}];
I have worked with Nuzhat Zari code, and I thank him for it, but also have experienced some issues with some core data operations between nested "sendAsynchronousRequest" (getting some weird thread and memory errors) so, my solution was unnest the calls to "sendAsynchronousRequest" and use some main thread variable validation.
#interface myMainThreadClass
#property (nonatomic,assign) NSInteger *currentAsyncTasks;
#end
#implementation
// Use init or viewDidLoad to make "currentAsyncTasks=0"!!
-(void)method
{
[self showLoadingAlert]; //or some ui update function
currentAsyncTasks++;
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//do something with data
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self dismissAlertInFinalTask]
});
}];
currentAsyncTasks++;
[NSURLConnection sendAsynchronousRequest:urlRequest2 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//do something with data
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self dismissAlertInFinalTask]
});
}];
currentAsyncTasks++;
[NSURLConnection sendAsynchronousRequest:urlRequest3 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//do something with data
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self dismissAlertInFinalTask]
});
}];
}
-(void)dismissAlertInFinalTask
{
currentNetworkTasks--;
if (currentNetworkTasks == 0)
{
[self dismissLoadingAlert];//or some ui update function;
}
}
#end
I also want to know if someone has managed to do multiple request using NSURLConnection delegate NSURLConnectionDataDelegate and ui calls.

how to show activity indicator in iphone application while uploading data

I am uploading data to server i want that when i click upload button then activity indicator should start and stop after the data is uploaded.
This is how i am uploading data but activity indicator not working.
-(IBAction)startSyncButtonAction{
[self startActivity];
CereniaAppDelegate *appDelegate = (CereniaAppDelegate *)[[UIApplication sharedApplication] delegate];
for (int i=0; i<[appDelegate.coffeeArray count]; i++) {
Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:i];
int mycount=[appDelegate.coffeeArray count];
NSLog(#"My Array count is %d",mycount);
NSString*device_Id=coffeeObj.device_Id;
NSString*R1=coffeeObj.R1;
NSString*R2=coffeeObj.R2;
NSString*R3=coffeeObj.R3;
NSString*R4=coffeeObj.R4;
NSString*R5=coffeeObj.R5;
NSString*R6=coffeeObj.R6;
NSString*R7=coffeeObj.R7;
NSString*R8=coffeeObj.R8;
NSString*R9=coffeeObj.R9;
NSString*R10=coffeeObj.R10;
NSString*R11=coffeeObj.R11;
NSString*R12=coffeeObj.R12;
NSString*R13=coffeeObj.R13;
NSString*R14=coffeeObj.R14;
NSString*update_date_time=coffeeObj.update_date_time;
NSString*teritory1=coffeeObj.teritory;
int mycount1=[appDelegate.coffeeArray count];
NSLog(#"My Array After delete is %d",mycount1);
NSLog(#"device_Id%#",device_Id);
NSLog(#"R1%#",R1);
NSLog(#"R2%#",R2);
NSLog(#"R3%#",R3);
NSLog(#"R4%#",R4);
NSLog(#"R4%#",R5);
NSLog(#"R4%#",R6);
NSLog(#"R4%#",R7);
NSLog(#"R4%#",R8);
NSLog(#"R4%#",R9);
NSLog(#"R4%#",R10);
NSLog(#"R4%#",R11);
NSLog(#"R4%#",R12);
NSLog(#"R4%#",R13);
NSLog(#"R4%#",R14);
NSLog(#"update_date_time%#",update_date_time);
NSString *post =[[NSString alloc] initWithFormat:#"device_Id=%#&R1=%#&R2=%#&R3=%#&R4=%#&R5=%#&R6=%#&R7=%#&R8=%#&R9=%#&R10=%#&R11=%#&R12=%#&R13=%#&R14=%#&update_date_time=%#&teritory1=%#",device_Id,R1,R2,R3,R4,R5,R6,R7,R8,R9,R10,R11,R12,R13,R14,update_date_time,teritory1];
NSLog(post);
NSURL *url=[NSURL URLWithString:#"http://celeritas-solutions.com/pah_brd_v1/pfizersurvey/SyncSurveySTD.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"%#",data);
}
for (int i=0; i<[appDelegate.coffeeArray count]; i++) {
Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:i];
[appDelegate removeCoffee:coffeeObj];
}
}
-(void)startActivity:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
activityImageView.hidden=NO;
activityView.hidden=NO;
[activityView startAnimating];
[pool release];
}
Try below code but I am giving example you should modify it & use according to your need:
alert= [[UIAlertView alloc] initWithTitle:#"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(150, 100);
[indicator startAnimating];
[alert addSubview:indicator];
Use this line to dismiss indicator:
[alert dismissWithClickedButtonIndex:0 animated:YES];
Try. Best of luck.
For better user experience i will recommend you to use UIProgressHUD
UIProgressHUD* hud = [[UIProgressHUD alloc] initWithFrame:CGRectZero];
[hud setText:#"Loading, please wait."];
[hud showInView:someView];
...
[hud done];
...
[hud hide];
...
[hud release];
Are you 100% sure that activityView is not nil?? I don't see anywhere you set it to anything. Also, even it were not nil, have you added it to a view? activityView is just a regular UIView, it won't automatically appear in UIWindows.

Both setCompletionBlock and setFailed don't get called

I'm getting acquainted with ASIHTTPRequest library and i tried to implement connection to a server. Here is my code:
- (BOOL)IsEnteredDataCorrect {
__block NSString *password;
__block NSString *responseString;
NSString *url = [NSString stringWithFormat:#"%#/login/",SERVER_URL];
__block ASIHTTPRequest *loginRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.labelText = #"Please wait";
[loginRequest setCompletionBlock:^
{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
responseString = [loginRequest responseString];
// NSLog(#"%#",responseString);
NSData *responseData = [loginRequest responseData];
NSError *err = nil;
NSDictionary *users = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&err];
password = [users objectForKey:idField.text];
}];
[loginRequest setFailedBlock:^
{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
[delegate alertError:[loginRequest error]];
NSLog(#"failed block");
}];
[loginRequest startAsynchronous];
NSLog(#"request is over");
if ([password isEqualToString:passwordField.text])
return YES;
else
return NO;
}
The server name is fake so i expected setFailedBlock to get called. But strange thing happens: MBProgressHUD is taken away (it is possible only when one of the blocks is called) but the code in setFailedBlock is not executed. The further code is executed successfully. OK, setFailedBlock doesn't work - but then MBProgressHUD should stay on the screen and it's gone. Can anyone explain me what's going on?
If your failed block isn't running but the MBProgressHUD is vanishing then your completion block is running.
Perhaps your server name isn't as fake as you think it is? There's an awful lot of weird domain names registered...
Also, your code here:
[loginRequest startAsynchronous];
NSLog(#"request is over");
if ([password isEqualToString:passwordField.text])
return YES;
else
return NO;
You seem to have a fundamental mismatch here - your are using an asynchronous request, but appear to be expecting that the answer is there immediately.

UIButton limiting number of presses

I got an issue with users rapidly pressing my UIButton causing multiple entries being placed in my database stored online. I have tried all sorts such as hiding the button when it the action is called and some sort of toggle, both have been unsuccessful. Is there anyway to limit the press to just one. the action is linked to the touch up inside reference on the button.
-(IBAction)postData:(id)sender
{
if(loginControl == 0)
{
if(nameIB.text.length && numberIB.text.length > 0)
{
loginControl = 1;
loginButton.hidden = YES;
NSMutableData *data = [NSMutableData data];
NSString *number = numberIB.text;
NSString *name = nameIB.text;
NSString *nameString = [[NSString alloc] initWithFormat:#"name=%#", name];
NSString *numberString = [[NSString alloc] initWithFormat:#"&number=%#", number];
NSString *genderString = [[NSString alloc] initWithFormat:#"&gender=%#", gender];
//NSLog(nameString);
//NSLog(numberString);
[data appendData:[nameString dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[numberString dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[genderString dataUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:#"http://www.blah.net/blah.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:data];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(#"responseData: %#", responseData);
userData = responseData;
[self startParsingUserId];
logoutButton.hidden = NO;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Text Fields Empty" message:#"One Or More Textfields Are Empty" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
loginControl = 0;
}
}
}
You should immediately be setting the enabled or hidden property of the button to disable interaction.
HOWEVER that change will not take effect until the next turn of the runloop, when everything gets re-drawn. As your code stands, your code is hiding the button, doing stuff, and then unhiding the button, all before the button gets a chance to redraw.
What you should do is set the hidden property and then start the computation (preferably on a background thread). Once and only once the computation completes, you should signal the main thread to un-hide the button again.
If you are OK with having this only work on iOS 4.0+, you can easily accomplish this with Grand Central Dispatch:
- (IBAction)doStuff:(id)sender {
[button setEnabled:NO];
dispatch_async(dispatch_get_global_queue(0,0), ^{
// do all your computation/synchronous requesting here
// this will happen on a background thread
dispatch_async(dispatch_get_main_queue(), ^{
[button setEnabled:YES];
});
});
}
In the button's selector, use -performSelectorOnMainThread:withObject:waitUntilDone: to run a chunk of code that disables the button until the method's logic is complete.

app is crashing due to tiny_malloc_from_free_list

Am working on an iPhone app,everything was going fine until I faced this crash, tiny_malloc_from_free_list. I am not sure why this crash is happening all of a sudden.Tried to google it but seems there aint enough suggestions to fix this one.
Please suggest any ideas how I can fix this problem.
Thanks,
Manoj
for reference here are some methods:
-(void) fetchProfileData
{
// Implemented ASIHTTP Request APIs
NSURL *url = [[NSURL URLWithString:SERVICE_NAME_PROFILEMANAGER] autorelease];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setValidatesSecureCertificate:NO];
[request setUsername:[[LoginManager sharedInstance] userName]];
[request setPassword:[[LoginManager sharedInstance] passWord]];
[request setDelegate:self];
[self.view addSubview:autheticateIndicator];
[autheticateIndicator startAnimating];
[authenticationLabel setHidden:NO];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
NSString *theXml = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
[NSThread detachNewThreadSelector:#selector(parseXML:) toTarget:self withObject:theXml];
[autheticateIndicator stopAnimating];
[theXml release];
}
-(void) parseXML:(NSString *)xmlData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[profilePersonnelNumber removeAllObjects];
// Load and parse the string data
tbxml = [[TBXML tbxmlWithXMLString:xmlData] retain];
// If TBXML found a root node, process element and iterate all children
if (tbxml.rootXMLElement) {
[self traverseElement:tbxml.rootXMLElement];
}
if ([profilePersonnelNumber count] > 0 ) {
NSDictionary *dict = [profilePersonnelNumber objectAtIndex:0];
if ([dict objectForKey:#"PersonnelNumber"])
{
[array release];
array = [[NSMutableArray arrayWithObjects:[dict valueForKey:#"PersonnelNumber"], nil] retain];
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
[result appendString:[obj description]];
[[LoginManager sharedInstance] personnelNumber:[obj description]];
}
[result release];
}
}
[tbxml release];
[pool release];
//TODO: Uncomment this part, must required
if ([profilePersonnelNumber count] > 0) {
UITabBarController *controller = self.tabBarController;
[self presentModalViewController:controller animated:YES];
}
}