multiple ASIHttpRequest takes time!!!!!!!!!!

Discussion in 'iOS Programming' started by AbhishekApple, Sep 21, 2010.

  1. AbhishekApple macrumors member

    Joined:
    Aug 5, 2010
    #1
    i have to fill 2 NSDictionary and an Array (which will bind to one Pickerview one at a time) on View load with the values returning from the server...

    I tried Replacing [request startSynchronous] with
    [request startAsynchronous] but it gives problem it fill only the
    array( arryColor) as the -(void)viewDidLoad executes completely first before the
    - (void)requestFinished:(ASIHTTPRequest *)request
    and makes the searchValue=Color

    Code:
    
    int flgTextField;
    static NSString *searchValue;
    
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    	flgTextField =1;
    	dictPrice = [[NSMutableDictionary alloc] init];
    	dictCategory = [[NSMutableDictionary alloc] init];
    	arryColor = [[NSMutableArray alloc] init];
    	
    	searchValue=@"Category";
    	[self fillPickerView:searchValue];
    	searchValue=@"Price";
    	[self fillPickerView:searchValue];
    	searchValue=@"Color";
    	[self fillPickerView:searchValue];
     
    	[pickerview reloadAllComponents];	
    }
    Code:
    -(void)fillPickerView:(NSString *)value{
    NSString *trackurl= [[NSString alloc] initWithFormat:@"http://abc.com/combolist=%@",value];  
    NSURL *url = [NSURL URLWithString:trackurl];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [trackurl release];
    [request setDelegate:self];
    [request [COLOR="Red"]startSynchronous[/COLOR]];
    }
    
    
    - (void)requestFinished:(ASIHTTPRequest *)request
    {
    NSString *responseString = [request responseString];
    NSArray *arry;
    if (searchValue==@"Category") {
    	arry= [responseString componentsSeparatedByString:@","];
    	for (int i=0; i<[arry count]; i++) {
    	NSArray *arryValue = [[arry objectAtIndex:i] componentsSeparatedByString:@"~"];
           [dictCategory setObject:[arryValue objectAtIndex:1] forKey:[arryValue objectAtIndex:0]];
    				
    			}
    
    		}
    		else if (searchValue==@"Price") {
    			arry= [responseString componentsSeparatedByString:@","];
    			for (int i=[arry count]-1; i>=0; i--) {
    				NSArray *arryValue = [[arry objectAtIndex:i] componentsSeparatedByString:@"~"];
    				[dictPrice setObject:[arryValue objectAtIndex:1] forKey:[arryValue objectAtIndex:0]];
    			}
    			
    		}
    		else if (searchValue==@"Color") {
    			arry = [responseString componentsSeparatedByString:@"~"];
    			[arryColor setArray:arry];
    		}
    		
    }
    
    Pickerview functions

    Code:
    - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    	switch (flgTextField) {
    		case 1:
    			return [dictPrice count];
    		
    		case 2:
    			return [dictCategory count];
    		
    		case 3:
    			return [arryColor count];
    		
    		case 4:
    			return [arrySort count];
    			
    		default:
    			return 0;
    		
    	}
    
    }
    
    
    - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    	switch (flgTextField) {
    		case 1:
    			return [dictPrice objectForKey:[[dictPrice allKeys] objectAtIndex:row]];
    			break;
    		case 2:
    			return [dictCategory objectForKey:[[dictCategory allKeys] objectAtIndex:row]];
    			break;
    		case 3:
    			return [arryColor objectAtIndex:row];
    			break;
    		case 4:
    			return [arrySort objectAtIndex:row];
    			break;
    		default:
    			return [NSString stringWithString:@"Not Specified"];
    			break;
    	}
    
    }
    #pragma mark TextField view
    Code:
    
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
    	// Make a new view, or do what you want here
    		flgTextField = textField.tag;
    		[pickerview reloadAllComponents];
    		return NO;	
    }
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    Indeed, network transactions do take time -- potentially a very long time (consider poor network connectivity, high server load etc.).

    Code:
    - (void)viewDidLoad {
    	...
    	searchValue=@"Category";
    	[self fillPickerView:searchValue];
    	searchValue=@"Price";
    	[self fillPickerView:searchValue];
    	searchValue=@"Color";
    	[self fillPickerView:searchValue];
    	...
    
    Using asynchronous requests is the right thing to do. (Otherwise the UI will not be responsive.)

    However, rather than relying on "searchValue" in requestFinished, you might instead consider inspecting the request URL in order to figure out what to do. (This will make the code more generic -- and it will not matter if the asynchronous replies arrive out of order.)

    Good luck :)
     
  3. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #3
    Thanks for reply.
     

Share This Page