View Full Version : Using cache with QNetworkAccessManager

7th December 2011, 13:14
Hi all,

I can't manage to make the cache work with WNAM.
Here is what I did so far :

MyNetworkManager* manager = new MyNetworkManager(parent);
QNetworkDiskCache* diskCache = new QNetworkDiskCache(parent);
QString dataPath = QDesktopServices::storageLocation(QDesktopServices ::CacheLocation);
diskCache->setMaximumCacheSize(5*1024*1024); // 5Mo

MyNetworkManager is very simple and redefines :

QNetworkReply *
MyNetworkManager::createRequest( Operation op, const QNetworkRequest & req, QIODevice * outgoingData)
QNetworkRequest request(req);
request.setAttribute(QNetworkRequest::CacheLoadCon trolAttribute, QNetworkRequest::PreferCache);
return QNetworkAccessManager::createRequest(op, request, outgoingData);

I use this manager in a Symbian QML application. I can see all requests goes through my createRequest function, which is fine.
I can also see that .cache files are created in the cache directory and they contains the responses data.
However, when I switch the internet connection off, the manager does not return cached data.

What am I doing wrong here ? Can somebody point me on a working exemple of using cache with QNAM (preferably with QML but pure Qt is also ok).

Thank you for your help !


7th December 2011, 13:48
AFAIR, to work offline you should use QNetworkRequest::AlwaysCache as cache load control.

7th December 2011, 15:16
Thanks Oleg, now it's working offline !
However I don't understand what PreferCache is for ? why doesn't it return cache if network is down ?

The only problem now is to detect whether device is online or offline to switch between AlwaysCache and PreferCache.
I thought QNAM::networkAccessible would do the trick but it always returns UnknownAccessibility on Symbian.
Do you have other ideas ?


7th December 2011, 15:28
From the docs:

By default the value of this property reflects the physical state of the device.

So, even on desktop it doesn't inform you about actual network availability. It seems Symbian version lacks even such functionality.

You can not switch between AlwaysCache and PreferCache, but use PreferCache and call QNetworkAccessManager::setNetworkAccessible() when network is down. To determinate this look for related Symbian API.