iOS8: NSURLSession: NSURLSessionDataTask 'Client closed connection before receiving entire response' - ios8

I use iOS BACKGROUND FETCH n BACKGROUND TRANSFER to update our app.
When we download an MP4 as part of a large download of lots of files the download can stop suddenly and get error:
'Client closed connection before receiving entire response'
http code is still 200.
We have no limit on data due to corporate cellular accounts and have single apps on our iPad so management want us to download a larger amount of data and number of files sometime over 20/30 on a clean install.
They're corporate videos/ pdfs published daily.
I created a NSURLSession
NSURLSession * backgroundSession_ = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier]
Then for each URL to the mp4 or PDF I create a NSURLSessionDownloadTask from my NSURLSession
NSURLSessionDownloadTask * downloadTask_ = [backgroundSession_ downloadTaskWithURL:url_];
The fire off resume on each download task.
When all return the NSURLSession completes and I tell the user in a notification that the apps has been updated.
I use CHARLES PROXY on my mac to monitor the downloads.
It takes a while as there are many files and also I noticed the movies seem to stream so can be slow.
Usually it checks whats been downloaded and only downloads the latest but if I do a lot of downloads say to a clean install I noticed that quiet often the streams to the mp4 get killed.
They have a 200 code but with message
'Client closed connection before receiving entire response'
Any idea what may cause this because it can stop my NSURLSession from completing so my sync can get in a sort of hung state.
IMAGE: OK DOWNLOAD: MOVIE STREAMS AND SAVED TO DISK: 200: Complete
IMAGE: DOWNLOAD SUDDENLY STOPS:
Any ideas: I use Background Fetch so not sure if the client in the message is iOS8 or my app.
After each downloadtask returns I copy the tmp file to mp4 in documents and call
[session finishTasksAndInvalidate];
if some of these downloadtask fail then I notice the session never finishes and gets stuck.
When I do another fetch I get all these old download task and old sessions.

was asked how I solved this but two years laster.
We also shrunk the movie files as well to speed up downloads.
I also remember the session and the configuration having separate settings. I had set header in one but failed when iOS 8/9 came out as it should have been set in the session not just the config - but may be fixed now as iOS 10 out
//-----------------------------------------------------------------------------------
//some movie streams time out n get
//'http://stackoverflow.com/questions/23428793/nsurlsession-how-to-increase-time-out-for-url-requests'
//'Client closed connection before receiving entire response'
NSLog(#"urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest:%f", urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest);
NSLog(#"urlSessionConfigurationBACKGROUND_.timeoutIntervalForResource:%f", urlSessionConfigurationBACKGROUND_.timeoutIntervalForResource);
//was 60
//urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest = 240.0;
urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest = 600.0;
// urlSessionConfigurationBACKGROUND_.timeoutIntervalForResource = 60.0;

You can ask your server mate to change the content-type to video/mpeg4 or you can change your request type from GET to POST

Related

In Xcode 14 & iOS 16 - purple warning when downloading an image from URL with NSData dataWithContentsOfURL

In Xcode 14 & iOS 16 - purple warning when downloading an image from URL with NSData dataWithContentsOfURL
Synchronous URL loading of <URL> should not occur on this application's main thread as it may lead to UI unresponsiveness. Please switch to an asynchronous networking API such as URLSession.
How to resolve this issue? Is anyone facing this same issue and resolving it?
As the warning and documentation say, the method involves work that shouldn't be performed on the main thread. In your case it seems you're trying to download remote data using this method. It takes time, sometimes more than expected, and sometimes doesn't finish at all.
For downloading data from internet, you can use NSURLSession and its tasks (e.g., this one). In this case, the work is dispatched to one of background threads, and when downloading finishes, you're notified in the completionHandler.
If your UI expects retrieving data synchronously, you should reconsider. UI should show something else until data is downloaded, allowing the user to interact with the app. Only after the download is done, UI should be updated accordingly.
Running the method into a separate thread resolved this issue.
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:#selector(DownloadFile:) object:objects];
[thread start];

Xamarin.Forms - Akavache how to recreate Sqlite3 connection after BlobCache.Shutdown()

I want the user to be able to leave the App and to come back as many times as he wants. Saving its data in the cache each time the app is sent background.
When my App on Xamarin.Forms goes "OnSleep()" the following code is performed:
BlobCache.LocalMachine.Flush().Wait();
await BlobCache.LocalMachine.InsertObject("test",value);
BlobCache.Shutdown().Wait();
The first time, it works.
But when the app is called to the front and then is resent to background, then it failed.
I notice, "BlobCache.Shutdown().Wait();" kills the BlobCache.LocalMachine's connection to Sqlite3.
And when the App is back to the front, this connection is not recreated.
Do you know, how to recreate this connection ?
Best regards

$mailer->addAttachment() not sending (or timing out) large files

Currently I am able to send attachments using Joomla
$mailer = JFactory::getMailer();
$config = JFactory::getConfig();
...
$mailer->addAttachment($filepath);
I am able to send attachments of very small sizes (I've tested 1-100KB of different file types INCLUDING .pdf) but when I tried to attach a 1.7mb pdf file. The page stops loading after some seconds then white screen. I tried to check in developer mode and it says ("Failed to load response data.").
Do you guys know how to go about this? Is this a max upload issue or the request just timed-out maybe because of the slow upload?
Thanks in advance.

Android Webview - can't disable cache

I'm using webview in my app which is loading remote web page, which is then using socket.io (node.js) via xhr-pooling.
Problem is that I can't disable caching of received data through socket.io.
For example, every 10 seconds my node server does io.emit, and my webview receives it and saves it in:
/data/data/...../webviewCache
I do not want my webview anything to save, because as time passes number of those files is just rising and they aren't helping my app run faster...
I've tried:
browser.getSettings().setCacheMode(2); //(2 is LOAD_NO_CACHE)
browser.getSettings().setAppCacheEnabled(false);
but neither of those works. My webview is still saving files to the cache folder.
At this moment, I've set up timer which is emptying cache folder every 60 seconds but that's not solution I would like to release in production...
Am I missing something here or there is bug with disabling cache within android?
UPDATE 1: After whole day of debugging I've found out something interesting.
Logcat shows two interesting things: saveCacheFile and getCacheFile
Then I've decided once again to try turn off the cache...
browser.getSettings().setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);
That actually caused that WebView wasn't loading files from cache anymore, but it was still saving them. Log cat says something like this:
saveCacheFile for url .../socket.io/1/xhr-polling/BLNN28E7S4PZJsy2pWaF?t=13537
So I believe actual question would be, how to prevent webview from SAVING cache files on every request.
How about adding random string in the query part of your URL? This trick works under some cases.
The only solution I found was to send "Cache-Control: no-store" in the HTTP response header.

ASIHTTPRequest Error, Code 2, on actual iPad

I have a program that asynchronously downloads 4 files into the Documents folder of an ipad, and this works perfectly on the simulator, but when I use it on an actual ipad it gives me this error:
Error Domain=ASIHTTPRequestErrorDomain Code=2 "The request timed out" UserInfo=0x1c0810 {NSLocalizedDescription=The request timed out}
Is there any way to fix this, or extend the time until time out?
The reason why you are getting better results in the simulator is probably because the simulator uses the built in network card and your Mac's network card is likely stronger than the one in the iPad.
You can use the following code to change the timeout interval. This code will set the timeout to twenty seconds:
[request setTimeOutSeconds:20]
According to the ASIHTTPRequest "How to Use" page, you can tell ASIHTTPRequest to try again N times, like so:
[request setNumberOfTimesToRetryOnTimeout:2];
EDIT:
Although it's 2016 and ASIHTTPRequest is long since deprecated, there's another useful bit of information to help diagnose networking issues on iOS devices. Try using the Network Link Conditioner. It ships with iOS and can be downloaded on macOS.
This article (link) from NSHipster has some great information on getting set up.
Cause of this problem may be the following. When we send a httprequest and don't received response from server but at this time you send another httprequest again and with second request response is received before first request.

Resources