Asynchronous calls within synchronous methods

Discussion in 'iOS Programming' started by arnieterm, Jan 4, 2010.

  1. arnieterm macrumors regular

    Joined:
    Aug 28, 2008
    #1
    How to make an asynchronous call embedded within a synchronous method so that the method can wait for a response in asynchronous call and proceed only when it receives a response. I am looking for an alternate to avoid using while loops.
    As the asynchronous request/response are scheduled through run loops so how can we solve this problem?
    Thanks
    Arnieterm
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Split the sync method into two and have the async callback that tells you it's complete call the second part of the method?

    If that doesn't work you'll have to be a lot more specific about the calls you are making (post code)
     
  3. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #3
    If you can't break the portions of your app which depend on this call into asynchronous state-driven blocks (because your porting legacy code, etc.), another option is to do the async call sequences in a run loop in another thread, then have the existing synchronous thread block on a semaphore until the async call thread has reached some end state.
     
  4. arnieterm thread starter macrumors regular

    Joined:
    Aug 28, 2008
    #4
    The asynchronous portion is related to NSStream
    Code:
    - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode{	
    
    The problem is that a method say "GetData" [Being a synchronous] with return type NSArray* initiates streaming procces with below given code

    Code:
    
    isStreamErrorEncountered = NO;	
    	[NSStream getStreamsToHostNamed: hostName port: hostPort inputStream: &iStream outputStream:&oStream];
    	[iStream retain];
    	[oStream retain];
    	[iStream setDelegate: self];
    	[oStream setDelegate: self];
    	[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
    	[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
    	[oStream open];
    	[iStream open];
    
    
    Here "iStream", "oStream" are static NSInputStream and NSOutputStream variables.
    When I get the response via the event handler [or time out whichever earlier] only then I need to return NSArray from the synchronous method.
    Can you explain your suggestion using a simple example

    Thanks
    Arnieterm
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    I would suggest reworking your execution logic so that GetData does not require the return of the NSArray but instead some other method is responsible for the NSArray's population. Then you can probably use one of the delegate methods of an async operation to trigger what is done once the NSArray is populated. Take a close look at the SeismicXML sample app which uses this approach quite nicely.
     
  6. arnieterm thread starter macrumors regular

    Joined:
    Aug 28, 2008
    #6
    Thanks for your reply. I will try to redesign my logic. So far I am stick to this situation as I have to make changes in an existing application that is making synchronous web service calls. Now that code has to include this async stuff too. Can you tell what can be the maximum size of input/output buffer though I am using 1024 at present.

    Thanks
    Arnieterm
     

Share This Page