From bf57c0e9d0287b0af7573b8e204e393d3a55dff4 Mon Sep 17 00:00:00 2001 From: tobrun Date: Tue, 3 Dec 2019 21:47:11 +0100 Subject: [android] only cancel request when needed --- platform/android/src/http_file_source.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/platform/android/src/http_file_source.cpp b/platform/android/src/http_file_source.cpp index 7066918fef..2ed5e844eb 100644 --- a/platform/android/src/http_file_source.cpp +++ b/platform/android/src/http_file_source.cpp @@ -38,6 +38,7 @@ private: Resource resource; FileSource::Callback callback; Response response; + bool executed = false; util::AsyncTask async { [this] { // Calling `callback` may result in deleting `this`. Copy data to temporaries first. @@ -97,10 +98,11 @@ HTTPRequest::HTTPRequest(jni::JNIEnv& env, const Resource& resource_, FileSource HTTPRequest::~HTTPRequest() { android::UniqueEnv env = android::AttachEnv(); - static auto& javaClass = jni::Class::Singleton(*env); - static auto cancel = javaClass.GetMethod(*env, "cancel"); - - javaRequest.Call(*env, cancel); + if (!executed) { + static auto& javaClass = jni::Class::Singleton(*env); + static auto cancel = javaClass.GetMethod(*env, "cancel"); + javaRequest.Call(*env, cancel); + } } void HTTPRequest::onResponse(jni::JNIEnv& env, int code, @@ -108,7 +110,7 @@ void HTTPRequest::onResponse(jni::JNIEnv& env, int code, const jni::String& cacheControl, const jni::String& expires, const jni::String& jRetryAfter, const jni::String& jXRateLimitReset, const jni::Array& body) { - + executed = true; using Error = Response::Error; if (etag) { @@ -163,6 +165,7 @@ void HTTPRequest::onResponse(jni::JNIEnv& env, int code, } void HTTPRequest::onFailure(jni::JNIEnv& env, int type, const jni::String& message) { + executed = true; std::string messageStr = jni::Make(env, message); using Error = Response::Error; -- cgit v1.2.1