Pregunta

I have working code but I need advice/direction if there is a better approach or if problems will arise using my current approach. The MBProgressHUD starts in the viewDidLoad, I then have a JSON method that posts and receives a response. It is a synchronous task because I need the information to modify labels on the screen. At the end of the JSON method is a call to stop the MBProgressHUD.

My viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //some code missing


    //start loading
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo: self.view animated:YES] ;
    hud.labelText =@"Loading Information";
    hud.detailsLabelText=@"Please wait.";
    hud.dimBackground = YES;

}

My viewDidAppear:

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    [self getJSON];
}

My getJSON method:

-(void) JSON{

    //post
    NSMutableString * postString = [NSMutableString stringWithString:homeUrl];
    [postString appendString:[NSString stringWithFormat:@"?%@=%@",@"email",self.email]];
    [postString appendString:[NSString stringWithFormat:@"&%@=%@",@"pass",self.pass]];
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
    [request setHTTPMethod:@"POST"];
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

    //get response
    NSString * requestST = [[request URL] absoluteString];
    NSData * jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:requestST]];
    NSError *error;

    //added check
    if (jsonData!=nil) {

       NSDictionary * dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

       self.status = [dataDictionary objectForKey:@"status"];
       self.balance = [dataDictionary objectForKey:@"result"];



       //check status
        if ([self.status isEqualToString:@"fail"]) {
       NSLog(@"Fail")
        }
    else{
        //assign variables
    }

}
//if JSON is NIL
else{
    NSLog(@"JSON Data is NIL");

}

//finish loading
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];

}

The architecture of the app is the following:

HOME-->Login-->Sign Up

Therefore I cannot use viewDidLoad in the Home method for the JSON call because it crashes. I uses popToRootViewController when there is a successful login. Just incase anyone ask why I use viewWillAppear for the JSON call. If there are any alternatives do not hesitate to suggest :)

¿Fue útil?

Solución

It is a synchronous task because I need the information to modify labels on the screen.

That's not a good excuse. If you run synchronous networking code on the main thread, you block the UI and the user interface hangs.

In viewDidLoad, set up your view hierarchy with whatever user interface is appropriate to show that the content is loading. Then, when your JSON finishes loading, update the user interface to show the content.

Otros consejos

You are using NSURLConnection so it's simple approach is

So start MBProgrssHUD in the NSURLConnection delegate method

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

and stop MBProgrssHUD in the NSURLConnection delegate method

- (void)connection:(NSURLConnection *)connection  didFailWithError:(NSError *)error

and

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top