RequestBuilder.into()->RequestManager.trank()->RequestTracker.runRequest()->SingleRequest.begin()
->onSizeReady()->最终到Engine.load()方法:
主要代码如下:
EngineKey key =keyFactory.buildKey(model, signature, width, height, transformations,
resourceClass, transcodeClass, options);
//从弱引用中取
EngineResource active = loadFromActiveResources(key, isMemoryCacheable);
if (active !=null) {
cb.onResourceReady(active, DataSource.MEMORY_CACHE);
if (VERBOSE_IS_LOGGABLE) {
logWithTimeAndKey("Loaded resource from active resources", startTime, key);
}
return null;
}
//从LRUcache中取,取到了移动到弱引用中去
EngineResource cached = loadFromCache(key, isMemoryCacheable);
if (cached !=null) {
cb.onResourceReady(cached, DataSource.MEMORY_CACHE);
if (VERBOSE_IS_LOGGABLE) {
logWithTimeAndKey("Loaded resource from cache", startTime, key);
}
return null;
}
//下载逻辑
接上一步,如果在缓存里没找到,则会进行下载流程:
engineJob.start(decodeJob)----> DecodeJob decodeJob .run()->runWrapped()->startNext()>SourceGenerator.startNext()->
loadData.fetcher.loadData(helper.getPriority(), this); //这步根据不同Fetcher来选择处理方式,
这里看HttpUrlFetcher,里面最终执行loadDataWithRedirects()方法看到了我们熟悉的urlConnection,
下载完数据后,回调callback.onDataReady(result);方法返回数据
SourceGenerator.onDataReady()方法会判断diskCacheStrategy缓存策略,如果可以混存则记录下来(这块不是太理解),否则执行DecodeJob.onDataFetcherReady(),该方法会判断是否当前线程,如果不是则改变runReason = RunReason.DECODE_DATA状态,执行extor最终会执行本地run方法,再次调用run()->runWrapped()方法,这次是DECODE_DATA流程,则执行DecodeJob.decodeFromRetrievedData()方法->notifyEncodeAndRelease()->notifyComplete()->回到EngineJob.notifyCallbacksOfResult()->最终调用回
Engine.onEngineJobComplete();
@SuppressWarnings("unchecked")
@Override
public synchronized void onEngineJobComplete(
EngineJob engineJob, Key key, EngineResource resource) {
// A null resource indicates that the load failed, usually due to an exception.
if (resource !=null) {
resource.setResourceListener(key, this);
if (resource.isCacheable()) {//缓存处理
activeResources.activate(key, resource);
}
}
jobs.removeIfCurrent(key, engineJob);
}
其他:
配置下载fetcher, 调用的时候Glide.with(context)会调用build流程初始化下载参数
其他优秀博客 :