0

I have a rails 3.0.3 website that, in development mode, responds to a HTTP GET by calling didReceiveData and then connectionDidFinishLoading, and photo data is sent from the website to the iPhone and displayed correctly. But when I run the rails 3.0.3 website in production mode on Amazon Web Services, connectionDidFinishLoading is called before didReceiveData. I'm using carrierwave to handle the photos for both the development and production rails sites.

Does anyone have any idea what could cause connectionDidFinishLoading to get called before didReceiveData?

I've tried checking the following:

1) I made sure the NSURLConnectionDelegate is called for the class' interface.

2) didReceiveResponse doesn't get called before connectionDidFinishLoading and the statusCode = 200.

3) In the rails app I am using send_file to send the photo data, and the path to the file sent in production, is an actual photo.

4) Checking with the full request to the website showed that 0 bytes of data was being returned from the production website by using the code below in connectionDidFinishLoading (this turned out to be the reason that didReceiveData wasn't being called):

NSData *returnedData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://production_website"]];

Here is the code for didReceiveData:

// theConnectionData is an instance variable defined as:

      NSMutableData  *theConnectionData = [[NSMutableData data] retain];


-(void)connection:(NSURLConnection *)con didReceiveData: (NSData *)data {

[theConnectionData   appendData:data ];

}

Update - I'm thinking the problem might be on the rails side. Here is the code I use to send a photo from the rails app from local storage on AWS to the iPhone app:

  @p       = Photo.find_by_user_id(user_id)
  uploader = @p.avatar
  uploader.retrieve_from_store!(File.basename(@p.avatar.url))
  uploader.cache_stored_file!

      send_file(uploader.file.path,
                :disposition   => 'inline',
                :encoding      => 'binary',
                :type          => @p.content_type,
                :stream        => false,
                :filename      => URI.encode(@p.filename),
                :x_sendfile    => true,
                :buffer_size   => 16384
                )

Anyone have any idea of why didReceiveData is not getting called before connectionDidFinishLoading for the rails production website?

James Testa
  • 2,901
  • 4
  • 42
  • 60
  • it should work.. but we can't test your code.If you can provide the code for didReceiveData. – Inder Kumar Rathore Feb 28 '12 at 03:21
  • Note that didReceiveData gets called by the development version of rails but does not get called by the production version of rails. – James Testa Feb 28 '12 at 13:23
  • can you give the production web sites address so that I can test...and it might be the problem in you site not in iphone coding – Inder Kumar Rathore Feb 28 '12 at 14:21
  • in production mode, is `didReceiveData` called even after `connectionDidFinishLoading` ? Actually, the server isn't calling anything, it's your NSURLConnection that inform its delegate of its status. are you fetching your photos one at a time ? maybe several NSURLConnection logs are overlapping in the console. – teriiehina Feb 28 '12 at 16:59
  • Yes, I am fetching the photos one at a time. That is an interesting thought, that requests could be overlapping. But if they were, how could connectionDidFinishLoading get called and didReceiveData is never called? Wouldn't didReceiveData get called before connectionDidFinishLoading for one of the photos? – James Testa Mar 01 '12 at 17:00

1 Answers1

0

I finally figured it out. Commenting out the following in config/environments/production.rb fixed the problem:

# config.action_dispatch.x_sendfile_header = "X-Sendfile"

The problem was also fixed by replacing the above line in config/environments/production.rb with the following:

config.action_dispatch.x_sendfile_header = "X-Accel-Redirect"

According to this post it looks like there were some changes to Rails 3 for send_file. This post suggested using the X-Accel-Redirect approach, but the post claims the request is then being sent from the rails app, instead of from nginx. This post claims that nginx requires the X-Accel-Redirect approach.

Community
  • 1
  • 1
James Testa
  • 2,901
  • 4
  • 42
  • 60