disable network cache in iOS
-
In my code I'm using HTTP get to communicate with a server. It appears that QNetworkAccessManager uses cache to return previously fetched data even if the device is offline. This behaviour seems to be specific to iOS. Here is my code:
QNetworkRequest request; request.setUrl(QUrl(urlStr)); request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork); request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false); m_reply = networkManager()->get(request); connect(m_reply, &QNetworkReply::finished, this, &CServerCommunicator::onLoginFinished);If urlStr has been fetched previously and the device is offline, m_reply emits
finished()with no error and contains cached data. On any other platform than iOS it finishes with network error as expected (I tried OS X, Ubuntu and Android).
What else can I try to disable network cache in iOS? I'm also callingnetworkManager()->cache()->clear();when my app is closing, but it also has no effect at all. -
The problem persists even if I set my own QNetworkDiskCache to the network manager and clear cache directory:
m_netManager = new QNetworkAccessManager(this); QNetworkDiskCache *diskCache = new QNetworkDiskCache(this); diskCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/" + QUuid::createUuid().toString()); m_netManager->setCache(diskCache);Actually I'm using a different directory each time, which should have the same effect. But no, network manager still returns data as if it were online.
-
Surprisingly, I was able to fix this by clearing the native iOS cache:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
This call has effect on both native iOS networking and Qt network code.In order to use the above call you have to rename your file to *.mm and include <Foundation/Foundation.h>.