Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-20-2011, 07:52 PM   #1 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default Convert network request from synchronous to asynchronous

I'm currently doing a synchronous network request to load my app which is not the best way as it blocks the UI. I'm also using ASIHTTPRequest. I understand I can just change the line from synchronous to asyncronous, but I think I need to setup a block so that I can trigger the table to reload once the async is completed.

The initial ViewController of my app:

Code:
    - (void)fetchAppointmentsForVisibleDate {
    	self.appointmentArray = [DrChronoDataSource getTodayData:self.visibleDate];
        
    	NSMutableArray *array = [NSMutableArray arrayWithCapacity:50];
    	for (NSDictionary *appointment in self.appointmentArray)
    	{
    		[array addObject: [NSString stringWithFormat:@"%@: %@", [appointment objectForKey:@"scheduled_time"], [appointment objectForKey:@"patient"]]];
    	}
    	self.listData = array;
        [self.appointmentTableView reloadData];
    }
In my app's dataSource class:

Code:
    + (NSMutableArray*)getTodayData:(NSDate*)today
    {
            NSURL *url = myURL here 
    		ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
    		[request setPostValue:[dateFormat stringFromDate:today] forKey:@"target_date"];
    		
    		[request startSynchronous];
    		
    		NSError *error = [request error];
    		NSString *responseString;
    		if (!error) {
    			responseString = [request responseString];
    		} else {
    			return NULL;
    		}
            return [responseString JSONValue];
    }
cityofangels is offline   Reply With Quote
Old 11-20-2011, 07:53 PM   #2 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default

I got this code but I'm getting a bunch of syntax errors, does anyone notice anything wrong? (ignore the pseudo code).

Code:
+ (void) getTodayData:(NSDate*)today callback:(void (^) (NSError * error, NSMutableArray * array)
{
        NSURL *url = myURL here 
        ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
        [request setPostValue:[dateFormat stringFromDate:today] forKey:@"target_date"];
        [request setCompletionBlock:^{
            callback(nil, [responseString JSONValue]);
        }];
        [request setFailBlock:^{
           callback([request error], nil);
        ];
        [request startAsynchronous];
}

- (void)fetchAppointmentsForVisibleDate {
    [DrChronoDataSource getTodayData:self.visibleDate callback:^(NSError *error, NSMutableArray *data) {
        if (!error) {
            self.appointmentArray = [data retain];
            NSMutableArray *array = [NSMutableArray arrayWithCapacity:50];
            for (NSDictionary *appointment in self.appointmentArray)
            {
                [array addObject: [NSString stringWithFormat:@"%@: %@", [appointment objectForKey:@"scheduled_time"], [appointment objectForKey:@"patient"]]];
            }
            self.listData = array;
            [self.appointmentTableView reloadData];
        }
        else 
            NSLog(@"%@", error.description);
    }];
}
cityofangels is offline   Reply With Quote
Old 11-20-2011, 08:02 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

What are your errors?

Specifically when using ASIHTTPRequest and blocks, you should prefix ASIHTTPRequest with "__block" (same goes for ASIFormDataRequest), like so:

Code:
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL urlWithString:@"http://www.google.com"]];
[request setCompletionBlock:^{
    NSLog(@"%@",[request responseString]);
}];
[request startAsynchronous];

Last edited by Speed; 11-20-2011 at 08:05 PM.
Speed is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 391
15 members and 376 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, matador1978, mbadegree, Murphy, QuantumDoja
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:32 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0