Glide4.9.0 DataCacheGenerator 源码解析

一、作用

/**
 * Generates {@link com.bumptech.glide.load.data.DataFetcher DataFetchers} from cache files
 * containing original unmodified source data.
 */

根据官方注释,关键词unmodified,就是我们获取到的没有修改过的数据。

二、源码解析

Glide4.9.0源码 整体概述(二)可以看到,DataCacheGenerator 的入口方法是startNext,所以我们可以先从这个方法入手。
可以分为2个步骤
步骤1,Glide4.9.0 getModelLoaders 源码解析解释了获取modelLoaders的过程,
步骤2,Glide4.9.0 SourceGenerator有LoadData相关介绍。

//DataCacheGenerator
@Override
  public boolean startNext() {
    //modelLoaders为null,或者modelLoaderIndex >=modelLoaders.size()
    while (modelLoaders == null || !hasNextModelLoader()) {
      sourceIdIndex++;
      if (sourceIdIndex >= cacheKeys.size()) {
        return false;
      }
	  //步骤1,遍历cacheKeys,尝试获取缓存FIle,并且通过getModelLoaders寻找是否有合适的modelLoaders。
      Key sourceId = cacheKeys.get(sourceIdIndex);
      Key originalKey = new DataCacheKey(sourceId, helper.getSignature());
      cacheFile = helper.getDiskCache().get(originalKey);
      if (cacheFile != null) {
        this.sourceKey = sourceId;
        modelLoaders = helper.getModelLoaders(cacheFile);
        modelLoaderIndex = 0;
      }
    }

	//步骤2,遍历moderLaders,生成loadData,判断是否可以处理该任务类型转换,
	//然后调用LoadData#DataFetcher#loadData开始任务。
    loadData = null;
    boolean started = false;
    while (!started && hasNextModelLoader()) {
      ModelLoader<File, ?> modelLoader = modelLoaders.get(modelLoaderIndex++);
      loadData =
          modelLoader.buildLoadData(cacheFile, helper.getWidth(), helper.getHeight(),
              helper.getOptions());
      if (loadData != null && helper.hasLoadPath(loadData.fetcher.getDataClass())) {
        started = true;
        loadData.fetcher.loadData(helper.getPriority(), this);
      }
    }
    return started;
  }

不过需要注意的是,Glide4.9.0 SourceGenerator一文中提到,SourceGenerator处理完毕后会移交给DataCacheGenerator,和直接通过DecodeJob#runGenerators启动DataCacheGenerator对比,虽然最终都会回调到DecodeJob。差别在于情况1的DataSource是通过LoadData#Fetcher#getDataSource获取的,情况2是直接赋值DataSource.DATA_DISK_CACHE

//SourceGenerator.java
//通过LoadData#Fetche#getDataSourcer获取
@Override
  public void onDataFetcherReady(Key sourceKey, Object data, DataFetcher<?> fetcher,
      DataSource dataSource, Key attemptedKey) {
    // This data fetcher will be loading from a File and provide the wrong data source, so override
    // with the data source of the original fetcher
    cb.onDataFetcherReady(sourceKey, data, fetcher, loadData.fetcher.getDataSource(), sourceKey);
  }

  @Override
  public void onDataFetcherFailed(Key sourceKey, Exception e, DataFetcher<?> fetcher,
      DataSource dataSource) {
    cb.onDataFetcherFailed(sourceKey, e, fetcher, loadData.fetcher.getDataSource());
  }
//DataCacheGenerator.java
//直接赋值。
@Override
  public void onDataReady(Object data) {
    cb.onDataFetcherReady(sourceKey, data, loadData.fetcher, DataSource.DATA_DISK_CACHE, sourceKey);
  }

  @Override
  public void onLoadFailed(@NonNull Exception e) {
    cb.onDataFetcherFailed(sourceKey, e, loadData.fetcher, DataSource.DATA_DISK_CACHE);
  }

DataSource是什么呢,其实是一个枚举类型。标志了数据来源。

/**
 * Indicates the origin of some retrieved data.
 */
public enum DataSource {
  /**
   * Indicates data was probably retrieved locally from the device, although it may have been
   * obtained through a content provider that may have obtained the data from a remote source.
   */
  LOCAL,
  /**
   * Indicates data was retrieved from a remote source other than the device.
   */
  REMOTE,
  /**
   * Indicates data was retrieved unmodified from the on device cache.
   */
  DATA_DISK_CACHE,
  /**
   * Indicates data was retrieved from modified content in the on device cache.
   */
  RESOURCE_DISK_CACHE,
  /**
   * Indicates data was retrieved from the in memory cache.
   */
  MEMORY_CACHE,
}
发布了22 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u012591964/article/details/88658389
今日推荐