Results 1 to 4 of 4

Thread: Using cache with QNetworkAccessManager

  1. #1
    Join Date
    Dec 2010
    Posts
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Using cache with QNetworkAccessManager

    Hi all,

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

    Qt Code:
    1. MyNetworkManager* manager = new MyNetworkManager(parent);
    2. QNetworkDiskCache* diskCache = new QNetworkDiskCache(parent);
    3. QString dataPath = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
    4. diskCache->setCacheDirectory(dataPath);
    5. diskCache->setMaximumCacheSize(5*1024*1024); // 5Mo
    6. manager->setCache(diskCache);
    To copy to clipboard, switch view to plain text mode 

    MyNetworkManager is very simple and redefines :

    Qt Code:
    1. QNetworkReply *
    2. MyNetworkManager::createRequest( Operation op, const QNetworkRequest & req, QIODevice * outgoingData)
    3. {
    4. QNetworkRequest request(req);
    5. request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
    6. return QNetworkAccessManager::createRequest(op, request, outgoingData);
    7. }
    To copy to clipboard, switch view to plain text mode 

    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 !

    Rémy

  2. #2
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Using cache with QNetworkAccessManager

    AFAIR, to work offline you should use QNetworkRequest::AlwaysCache as cache load control.
    Oleg Shparber

  3. #3
    Join Date
    Dec 2010
    Posts
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Using cache with QNetworkAccessManager

    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 ?

    Thanks

  4. #4
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Using cache with QNetworkAccessManager

    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.
    Oleg Shparber

Similar Threads

  1. QWebKit enabling cache
    By Talei in forum Newbie
    Replies: 1
    Last Post: 26th April 2011, 17:06
  2. Cache of Credential
    By lyhoanghai in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2011, 11:15
  3. Clear a cache!
    By Indalo in forum Qwt
    Replies: 1
    Last Post: 5th March 2010, 16:59
  4. Replies: 1
    Last Post: 18th December 2009, 11:20
  5. using QWebView cache with QNetworkDiskCache
    By Jann3 in forum Qt Programming
    Replies: 3
    Last Post: 7th October 2009, 13:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.