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 09-12-2011, 05:49 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 53
pavlicky is on a distinguished road
Default Data fetcher using ASIHTTPRequest

Hi guys,

I have my fetcher class, which has couple of methods for fetching data from the internet, process them (creates object) and returns that object back to its view controller. I use asynchronous from ASIHTTPRequest.

I want to do the code in this fetcher very code reusable. Therefore I followed an example from ASIHTTPRequest doc for asynchronous loading:
PHP Code:
- (User *)loadUser:{
   
NSURL *url = [NSURL URLWithString:@"http://something.com"];
   
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [
request setDelegate:self];
   [
request startAsynchronous];
}
 
- (
void)requestFinished:(ASIHTTPRequest *)request{
   
NSString *responseString = [request responseString];
   
//[b]OBJECT CREATION[/b]
}
 
- (
void)requestFailed:(ASIHTTPRequest *)request{
   
NSError *error = [request error];

loadUser method of course doesn't work because there is no way how I can create and return User object. Unless I would have created it in - (void)requestFinishedASIHTTPRequest *)request. This would work but only for this particular ONE method loadUser.

Therefore, I have tried to use blocks - I dunno whether it is the best idea or not, but the code looks like this:
PHP Code:
- (User *)loadUser:(int)user_id{
    
__block User *user;
    
    
NSURL *url = [NSURL URLWithString:@"http://something.com"];
    
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [
request setDelegate:self];
    
    [
request setCompletionBlock:^{
        
NSString *responseString = [request responseString];
        
//[b]DATA SERIALIZATION[/b]
        
user = [[User allocinitWithUser_id:user_id];
        
NSLog(@"USER complete: %@", [user name]);    
    }];
    [
request setFailedBlock:^{
        
NSError *error = [request error];
        
NSLog(@"%@"error);
    }];
    [
request startAsynchronous];

    
NSLog(@"USER: %@", [user name]);
    return 
user;

I have used NSLogs to check the object. In the end of the method, User object is returned null. So I actually get null back to my view controller . Later on...when request setCompletionBlock is done, the User object is being created! BUT it is already too late, cause loadUser method has been executed and returned null to the view controller.

What should I do, if I want to have all request in the same fetcher class? And reuse method request setCompletionBlock? Or would it be better approach to fetch those requests directly in my view controllers? Thanks in advance for your help
pavlicky 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: 403
9 members and 394 guests
7twenty7, Eclectic, eski, fiftysixty, JackReidy, teebee74, tim0504, UMAD, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

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