Glide的源碼分析(三)

Glide取消圖片加載
1.在任務剛開始時;
2.在EngineJob中,Future.cancel(true)
3.在加載完成,但沒有加載到控件;
RequestManager.java:java

public void pauseRequests() {
        Util.assertMainThread();
        requestTracker.pauseRequests();
    }

RequestTracker.java:ide

public void pauseRequests() {
        isPaused = true;
        for (Request request : Util.getSnapshot(requests)) {
            if (request.isRunning()) {
                request.pause();
                pendingRequests.add(request);
            }
        }
    }

GenericRequest.java:this

@Override
    public void pause() {
        clear();
        status = Status.PAUSED;
    }

GenericRequest.LoadStatus.java:code

public void cancel() {
            engineJob.removeCallback(cb);
    }

EngineJob.java:圖片

public void removeCallback(ResourceCallback cb) {
        Util.assertMainThread();
        if (hasResource || hasException) {
            addIgnoredCallback(cb);
        } else {
            cbs.remove(cb);
            if (cbs.isEmpty()) {
                cancel();
            }
        }
    }
    void cancel() {
        if (hasException || hasResource || isCancelled) {
            return;
        }
        engineRunnable.cancel();
        Future currentFuture = future;
        if (currentFuture != null) {
            currentFuture.cancel(true);
        }
        isCancelled = true;
        listener.onEngineJobCancelled(this, key);
    }

EngineRunnable.java:rem

public void cancel() {
        isCancelled = true;
        decodeJob.cancel();
    }

EngineRunnable.java:get

@Override
    public void run() {
        if (isCancelled) {
            return;
        }

        Exception exception = null;
        Resource<?> resource = null;
        try {
            resource = decode();
        } catch (Exception e) {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, "Exception decoding", e);
            }
            exception = e;
        }

        if (isCancelled) {
            if (resource != null) {
                resource.recycle();
            }
            return;
        }

        if (resource == null) {
            onLoadFailed(exception);
        } else {
            onLoadComplete(resource);
        }
    }

EngineJob.java:requests

private volatile Future<?> future;
    void cancel() {
        if (hasException || hasResource || isCancelled) {
            return;
        }
        engineRunnable.cancel();
        Future currentFuture = future;
        if (currentFuture != null) {
            currentFuture.cancel(true);
        }
        isCancelled = true;
        listener.onEngineJobCancelled(this, key);
    }
public void start(EngineRunnable engineRunnable) {
        this.engineRunnable = engineRunnable;
        future = diskCacheService.submit(engineRunnable);
    }

    @Override
    public void submitForSource(EngineRunnable runnable) {
        future = sourceService.submit(runnable);
    }
相關文章
相關標籤/搜索