浏览代码

添加演示如何重复发起请求但取消旧的任务

drake 4 年之前
父节点
当前提交
dbee9d8dba

+ 30 - 0
sample/src/main/java/com/drake/net/sample/ui/fragment/UniqueRequestFragment.kt

@@ -0,0 +1,30 @@
+package com.drake.net.sample.ui.fragment
+
+import android.os.Bundle
+import android.util.Log
+import android.view.View
+import androidx.fragment.app.Fragment
+import com.drake.net.Post
+import com.drake.net.sample.R
+import com.drake.net.scope.AndroidScope
+import com.drake.net.utils.scopeNetLife
+import kotlinx.android.synthetic.main.fragment_unique_request.*
+
+class UniqueRequestFragment : Fragment(R.layout.fragment_unique_request) {
+
+    private var scope: AndroidScope? = null
+
+    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+
+        btn_request.setOnClickListener {
+            tv_result.text = "请求中"
+            scope?.cancel() // 如果存在则取消
+
+            scope = scopeNetLife {
+                val result = Post<String>("api").await()
+                Log.d("日志", "请求到结果") // 你一直重复点击"发起请求"按钮会发现永远无法拿到请求结果, 因为每次发起新的请求会取消未完成的
+                tv_result.text = result
+            }
+        }
+    }
+}

+ 10 - 0
sample/src/main/res/drawable/ic_unique.xml

@@ -0,0 +1,10 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24"
+    android:tint="?attr/colorControlNormal">
+    <path
+        android:fillColor="@android:color/white"
+        android:pathData="M19,13H5v-2h14v2z" />
+</vector>

+ 40 - 0
sample/src/main/res/layout/fragment_unique_request.xml

@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".ui.fragment.UniqueRequestFragment">
+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="50dp"
+        android:gravity="center_vertical"
+        android:paddingHorizontal="16dp"
+        android:text="重复发起相同网络请求时, 取消上次未完成请求"
+        android:textSize="12dp"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <Button
+        android:id="@+id/btn_request"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:text="发起请求"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <TextView
+        android:id="@+id/tv_result"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="32dp"
+        android:gravity="center"
+        android:paddingHorizontal="16dp"
+        android:textColor="@color/black"
+        app:layout_constraintTop_toBottomOf="@id/btn_request"
+        tools:text="请求结果" />
+
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 4 - 0
sample/src/main/res/menu/menu_main.xml

@@ -67,6 +67,10 @@
         android:id="@+id/fastest"
         android:icon="@drawable/ic_fastest"
         android:title="最快选择" />
+    <item
+        android:id="@+id/unique"
+        android:icon="@drawable/ic_unique"
+        android:title="唯一请求" />
     <item
         android:id="@+id/state_layout"
         android:icon="@drawable/ic_state_layout"

+ 5 - 0
sample/src/main/res/navigation/nav_main.xml

@@ -116,5 +116,10 @@
         android:name="com.drake.net.sample.ui.fragment.FastestFragment"
         android:label="最快请求"
         tools:layout="@layout/fragment_fastest" />
+    <fragment
+        android:id="@+id/unique"
+        android:name="com.drake.net.sample.ui.fragment.UniqueRequestFragment"
+        android:label="唯一请求"
+        tools:layout="@layout/fragment_unique_request" />
 
 </navigation>