Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. disable network cache in iOS

disable network cache in iOS

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 1 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    m_andrej
    wrote on last edited by m_andrej
    #1

    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 calling networkManager()->cache()->clear(); when my app is closing, but it also has no effect at all.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      m_andrej
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        m_andrej
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved