1

I have a web server that is streaming JSON results back asynchronously to an iOS client. The client is connecting using NSURLConnection and I access the data from the method:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData)

Data is currently coming back in 1024 byte chunks. However, I'm not sure how to tell if when I receive data if the message was complete other than appending all the data I receive to a string and try to parse it to JSON each time. This method seems quite error prone - is there a better way to handle this? Something that would mark in the headers or something when a full response has been sent?

lehn0058
  • 19,977
  • 15
  • 69
  • 109

2 Answers2

2

You have two ways

first & better way is implement connectionDidFinishLoading: NSURLConnectionDataDelegate delegate which will trigger when a connection has finished loading successfully.

Second way is handling it manually as follows.

You can do the following things in Web-server side,

Step1: Send the below informations first before starting to send the original data.

      a.Number of Chunks.[totalSize/1024] (mandatory).
      b.TotalSize(not mandatory).

You can do the following things in Client side side,

Step1: Store the above informations.

Step2: Write the below code

@property (nonatomic,assign) int chunkNumber;

@property (nonatomic,strong) NSData *receivedData;

Self.chunkNumber = 1;

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData)myata{

       if(self.chunkNumber != Number of Chunks)
       {
          if(!self.receivedData)
          {
            //allocate and initialize self.receivedData
          }

          [self.receivedData appendData:myData];    

       }
       else
       {

         //completed . do whatever with self.receivedData.
         //if you want to validate, just check the self.receivedData size with TotalSize

        self.chunkNumber = 1;

       }

    }
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • Method #1 will not work in my case, since I am streaming data back and leaving the async connection opened over a long period of time so the server can push results out to the client as they are available. The connectionDidFinishLoading method only appears to be called if the connection actually closes. I hoping to avoid implementing something like method #2, but perhaps that will be my best bet. – lehn0058 Mar 28 '13 at 13:01
1

In the NSURLConnectionDataDelegate, there is a method connectionDidFinishLoading: that should be called when the server is done sending. You can also retrieve the expected length in didReceiveResponse but that is not reliable and required server side support.

DrC
  • 7,528
  • 1
  • 22
  • 37
  • connectionDidFinishLoading will not work in my case, since I am streaming data back and leaving the async connection opened over a long period of time so the server can push results out to the client as they are available. The connectionDidFinishLoading method only appears to be called if the connection actually closes. – lehn0058 Mar 28 '13 at 12:56