Just My Life & My Work

[iOS] 不要緩存網路資料

原以為已經解決AFNetworking會暫存資料的問題,第二次驗收還是被資安公司給退件,那麼只好使出一勞永逸的絕招!就是讓整個App網路資料都不要暫存資料,也就是說不只是針對AFNetworking,設定NSURLCache就能做到!透過模擬器追蹤Cache資料夾,打開Cache.db查看真的不再暫存撈回來的資料。

Disable AFNetworking Cache00004

可以看到追蹤模擬器資料夾會有個Cache.db。

打開Cache.db會發現⋯⋯

Disable AFNetworking Cache00003

點進去查看,會發現每一次的回傳都會被記錄進資料庫呢!

Disable AFNetworking Cache00002

真的就能發現我的登入資訊。

Disable AFNetworking Cache00001

Apple的URLCache有提到:

By default, the Cocoa URL loading system uses a small shared memory cache. We don’t need this cache, so we set it to zero when the application launches.

建議在App啟動後就執行以下程式碼:

/**
 Theme: Disable Cache
 IDE: Xcode 8
 Language: Objective C
 Date: 106/06/16
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
                                                        diskCapacity:0
                                                            diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

AFNetworking作者Mattt自己來回答怎麼不暫存網路資料:

Cacheing is handled application-wide by NSURLCache. If you don’t set a shared cache, requests are not cached. Even with a shared NSURLCache, the default implementation on iOS does not support disk cacheing anyway.

That said, unless you have a very particular reason to write your own cacheing system, I would strongly recommend against it. NSURLCache is good enough for 99.9% of applications: it handles cache directives from incoming responses and uses them appropriately with new requests, and does so automatically in a way that is unlikely to be a performance bottleneck in your application. As someone who has wasted untold hours making one myself (and later throwing it away since it was completely unnecessary), I’d say that there are much better places to focus your development attention.

Disable AFNetworking Cache00005

看掉App重新編譯執行,就再也找不到Cache.db。

參考:How To Disable AFNetworking Cache

Comments on: "[iOS] 不要緩存網路資料" (12)

  1. 是否程式結束時也要在設定一次?
    不然依照你寫的邏輯,到你下次用為止
    Nsurlcache裡頭應該會有你上次用的資訊
    如有錯誤請指教^^

    仔細了解func功能後
    發現你把NSURLCache的記憶體空間設為0
    來達到無法暫存的目的
    上了一課^^

  2. 是否程式結束時也要在設定一次?
    不然依照你寫的邏輯,到你下次用為止
    Nsurlcache裡頭應該會有你上次用的資訊
    如有錯誤請指教^^

    仔細了解func功能後
    發現你把NSURLCache的記憶體空間設為0
    來達到無法暫存的目的
    上了一課^^

  3. ardalionanguiano said:

    是否程式結束時也要在設定一次?
    不然依照你寫的邏輯,到你下次用為止
    Nsurlcache裡頭應該會有你上次用的資訊
    如有錯誤請指教^^

    仔細了解func功能後
    發現你把NSURLCache的記憶體空間設為0
    來達到無法暫存的目的
    上了一課^^

  4. 是否程式結束時也要在設定一次?
    不然依照你寫的邏輯,到你下次用為止
    Nsurlcache裡頭應該會有你上次用的資訊
    如有錯誤請指教^^

    仔細了解func功能後
    發現你把NSURLCache的記憶體空間設為0
    來達到無法暫存的目的
    上了一課^^

  5. 是否程式結束時也要在設定一次?
    不然依照你寫的邏輯,到你下次用為止
    Nsurlcache裡頭應該會有你上次用的資訊
    如有錯誤請指教^^

    仔細了解func功能後
    發現你把NSURLCache的記憶體空間設為0
    來達到無法暫存的目的
    上了一課^^

  6. 是否程式結束時也要在設定一次?
    不然依照你寫的邏輯,到你下次用為止
    Nsurlcache裡頭應該會有你上次用的資訊
    如有錯誤請指教^^

    仔細了解func功能後
    發現你把NSURLCache的記憶體空間設為0
    來達到無法暫存的目的
    上了一課^^

  7. 是否程式結束時也要在設定一次?
    不然依照你寫的邏輯,到你下次用為止
    Nsurlcache裡頭應該會有你上次用的資訊
    如有錯誤請指教^^

    仔細了解func功能後
    發現你把NSURLCache的記憶體空間設為0
    來達到無法暫存的目的
    上了一課^^

    Liked by 1 person

  8. 是否程式結束時也要在設定一次?
    不然依照你寫的邏輯,到你下次用為止
    Nsurlcache裡頭應該會有你上次用的資訊
    如有錯誤請指教^^

    Liked by 1 person

隨意留個言吧:)~

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料

標籤雲