Skip to content

Commit

Permalink
Allow assign TAGs to RequestHandles and cancel requests that contain …
Browse files Browse the repository at this point in the history
…specific TAG
  • Loading branch information
smarek committed Jul 16, 2015
1 parent 908e251 commit 8b1243c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Expand Up @@ -20,7 +20,6 @@

import android.content.Context;
import android.os.Looper;
import android.util.Log;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
Expand Down Expand Up @@ -829,6 +828,30 @@ public void cancelAllRequests(boolean mayInterruptIfRunning) {
requestMap.clear();
}

/**
* Allows you to cancel all requests currently in queue or running, by set TAG,
* if passed TAG is null, will not attempt to cancel any requests, if TAG is null
* on RequestHandle, it cannot be canceled by this call
*
* @param TAG TAG to be matched in RequestHandle
* @param mayInterruptIfRunning specifies if active requests should be cancelled along with
* pending requests.
*/
public void cancelRequestsByTAG(Object TAG, boolean mayInterruptIfRunning) {
if (TAG == null) {
log.d(LOG_TAG, "cancelRequestsByTAG, passed TAG is null, cannot proceed");
return;
}
for (List<RequestHandle> requestList : requestMap.values()) {
if (requestList != null) {
for (RequestHandle requestHandle : requestList) {
if (TAG.equals(requestHandle.getTag()))
requestHandle.cancel(mayInterruptIfRunning);
}
}
}
}

// [+] HTTP HEAD

/**
Expand Down
20 changes: 20 additions & 0 deletions library/src/main/java/com/loopj/android/http/RequestHandle.java
Expand Up @@ -27,6 +27,7 @@
*/
public class RequestHandle {
private final WeakReference<AsyncHttpRequest> request;
private WeakReference<Object> TAG = null;

public RequestHandle(AsyncHttpRequest request) {
this.request = new WeakReference<AsyncHttpRequest>(request);
Expand Down Expand Up @@ -95,4 +96,23 @@ public boolean shouldBeGarbageCollected() {
request.clear();
return should;
}

/**
* Will set Object as TAG to this request, wrapped by WeakReference
*
* @param tag Object used as TAG to this RequestHandle
*/
public RequestHandle setTag(Object tag) {
TAG = new WeakReference<Object>(tag);
return this;
}

/**
* Will return TAG of this RequestHandle
*
* @return Object TAG, can be null
*/
public Object getTag() {
return TAG.get();
}
}

0 comments on commit 8b1243c

Please sign in to comment.