尝试吗?数据(contentsOf:URL)使用缓存?

时间:2019-09-06 17:52:30

标签: ios swift nsurlconnection

我看到一个问题,我用不同的URL多​​次运行以下内容,并且“有时”返回相同的数据(似乎是随机的)。我想知道iOS是否正在缓存某些页面并给我返回相同的数据,而不是实际转到URL。

有缓存吗?如果是这样,是否有办法在每次通话前清除它?

plugins {
    id("java-library")
    id("org.openapi.generator").version("4.0.1")
    id("org.springframework.boot").version("2.1.8.RELEASE")
}

repositories {
    jcenter()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web:2.1.8.RELEASE")
    api("io.springfox:springfox-swagger2:2.8.0")
    api("io.springfox:springfox-swagger-ui:2.8.0")
    api("org.openapitools:jackson-databind-nullable:0.1.0")
}

val spec = "$rootDir/docs/openapi/api.yml"
val generatedSourcesDir = "$buildDir/generated/openapi"

openApiGenerate {
    generatorName.set("spring")

    inputSpec.set(spec)
    outputDir.set(generatedSourcesDir)

    apiPackage.set("org.openapi.example.api")
    invokerPackage.set("org.openapi.example.invoker")
    modelPackage.set("org.openapi.example.model")

    configOptions.set(mapOf(
            "dateLibrary" to "java8"
    ))
}

sourceSets {
    getByName("main") {
        java {
            srcDir("$generatedSourcesDir/src/main/java")
        }
    }
}

tasks {
    val openApiGenerate by getting

    val compileJava by getting {
        dependsOn(openApiGenerate)
    }
}

1 个答案:

答案 0 :(得分:3)

看看init(contentsOf:options:),它提供了一个名为NSData.ReadingUncached的选项。这证明了简单表单已被缓存,这是如何控制其是否已缓存的答案。

通过URLSession

可实现更好控制的更好方法