Litho
  • Introduction
  • 简介
    • Litho 是什么
    • 动机
    • 使用
  • 快速开始
    • 新手入门
    • 教程
    • 编写 Component
    • 使用 Component
  • 参考
    • Layout Specs
    • Mount Specs
    • Props
    • State
    • Layout
    • Error Boundaries
    • Borders
    • RecyclerCollectionComponent
    • 变换动画
    • 术语
  • 事件处理
    • 总览
    • 触摸处理
    • 可见性处理
  • Sections
    • Sections 是什么
    • Sections 教程
    • GroupSection Specs
    • DiffSection Specs
    • Sections Building Blocks
    • 与UI交互
    • Sections 单元测试
    • Sections 与 Views
    • Services
    • Sections 架构
    • Working Ranges
  • 兼容性
    • Styles
    • Accessbility
    • RTL
  • 测试
    • 总览
    • 单元测试基础
    • 子组件测试
    • 匹配 Props
    • 测试 InjectProps
    • 测试事件处理
    • Espresso
    • 在Android Studio上运行测试
  • 高级指南
    • Recycler
    • 自定义布局
    • TreeProps
    • 增量安装
    • 创建 ComponentTree
  • 架构
    • 代码生成
    • 异步布局
    • 增量安装
    • 铺平 View 结构
    • 回收
  • 附加资源
    • 最佳实践
    • FAQ
  • 工具
    • 调试
    • 开发者选项
  • 更多参与
    • 如何参与
    • 社区展示
    • 资源库结构
Powered by GitBook
On this page
  1. Sections

Services

PreviousSections 与 ViewsNextSections 架构

Last updated 7 years ago

Service

数据流

数据在被渲染在屏幕上之前会流经 Sections 。 Sections 在数据源与 UI 之间高效计算组件渲染的变更集。

为了利用更优的性能,我们只须做需要做的工作。因此,理想情况下,只有 Section 需要数据的时候才获取数据。这就是Service 的工作。

介绍

Sections 使用 Services 命令式的控制数据源。通过 Service API , 可以在 Section 层级结构之外使用 。 这可以让你知道何时开始获取数据。

因为 Service 是被绑定到特定的 Section 上,也就是说 Service 可以使用所有 Prop 以及 State ,并和它们交互,也意味着 Service 可以对例如 @OnViewportChanged 和 @OnRefresh 等事件作出响应。当新数据抵达时,请求状态更新,让数据沿着 Section 层级结构流动 。

@OnCreateService

Services 存活于状态更新以及 Sections 停留在层级结构的期间。 第一个也是仅有的 Service 实例应该在生命周期方法 @OnCreateService 中创建。

@GroupSectionSpec
public ServiceSectionSpec {
  ...
  @OnCreateService
  static DataLoader onCreateServices(
    SectionContext c,
    @Prop Configuration config,
    ...) {
      /**
       * onCreateServices() is called only once when the Section is first created.
       * In this function you should create and return your service.
       **/
      return new DataLoaderFactory.createLoader(config);
  }
}

@OnBindService 与 @OnUnbindService

@OnBindService 是个回调方法,里面定义 Service 如何与 Section 进行交互。当数据加载完成,新的数据被传到 Section 中时调用这个方法。

@OnUnbindService 提供回调用于清理以及撤销在 @OnBindService 执行过的操作。

@GroupSectionSpec
public ServiceSectionSpec {
  ...
  @OnBindService
  static void onBindService(
    final SectionContext c,
    final DataLoader service,
    ...) {
      /**
       * onBindService() is called (along with onUnbindService()) every time
       * the section tree is updated (usually because of a state update).
       * This function is passed the service created by onCreateService as the second function parameter.
       * In this function you should bind any EventHandler that will make state changes to your service.
       **/
      service.registerEventLoader(ServiceSection.dataLoaded(c));
  }

  @OnUnbindService
  static void onUnbindService(
    final SectionContext c,
    final DataLoader service,
    ...) {
      /**
       * onUnBindService() is called (along with onBindService()) every time
       * the section tree is updated (usually because of a state update).
       * This function is passed the service created by onCreateService as the second function parameter.
       * This should be the inverse of onBindService(). Anything set or bound in onBindService() should be undone here.
       **/
      service.unregisterEventLoader();
  }

  @OnEvent(YourData.class)
  static void dataLoaded(
    final SectionContext c,
    @FromEvent final Data data) {
      // Update your state with the new data
      ServiceSection.updateData(c, data);
  }

  @UpdateState
  static void updateData(
    final StateValue<Data> connectionData,
    @Param Data data) {
      connectionData.set(data);
  }
}

数据获取

如上所述, services 可以对例如 @OnViewportChanged 和 @OnRefresh 等事件作出响应:

 @OnRefresh
 static void onRefresh(
   SectionContext c,
   DataLoader service,
   ...) {
     service.refreshData();
 }

 @OnViewportChanged
 static void onViewportChanged(
   SectionContext c,
   int firstVisibleIndex,
   int lastVisibleIndex,
   int totalCount,
   int firstFullyVisibleIndex,
   int lastFullyVisibleIndex,
   DataLoader service,
   ...) {
     service.makeTailFetch();
 }
SectionLifecycle