Skip to content

Instantly share code, notes, and snippets.

@matin0728
Last active March 29, 2017 06:33
Show Gist options
  • Save matin0728/9c14be6cb66735f6424dd03a9f96191c to your computer and use it in GitHub Desktop.
Save matin0728/9c14be6cb66735f6424dd03a9f96191c to your computer and use it in GitHub Desktop.
ck issue

CK 相关的东西

CK 的定位

一个视图渲染器.

特性:单向数据流,结果集是以增量的形式更新

主要原因: 将 CK 里面获得的 index 用于别处, 出问题的地方:

  1. indexPathForObject 实现有问题
     ```
      - (nullable NSIndexPath *)indexPathForObject:(nonnull NSObject *)object {

      ZHComment * comment = [self getCommentFromObject:object];
      if (!comment) {
          return nil;
      }
      CKTransactionalComponentDataSourceState *currentState = [self.ckDatasource valueForKey:@"_currentState"];
      ```

   2. 使用 datasource 来查找 model,会让数据更加同步

      ```
      return [self.ckDatasource modelForItemAtIndexPath:indexPath];
      ```

   3. 使用 ck 生成 removeAll 的 changeSet (可能有未提交的 changeset,导致 index 错误)

改进方向:

事件处理方法/工作流程:

  1. 在事件的 handler 中一般可以得到该 cell 绑定的 model,如果没有事先存储,此时可以使用 self.ckDataSource modelAtIndexPath
  2. 如果需要 「更新/删除」 此 cell ,则要在 viewModel 里面计算出它所在的位置转换成 changeSet 提交给 CK.

需要修改的实现:

  - 移除 viewModel 对 ckDataSource 的引用.
  - 去掉indexPathForObject 的实现
  - 对于 section 数量变更时的刷新操作

测试阶段可以采用的些策略来暴露异步操作导致的不一致性

1. 慢速网络
2. 增加每次返回的数据集,比如 x5, x10
3. 针对性的模拟数据测试:比如第一屏只返回精选评论

其它

同步和异步提交 changeSet 会怎么样,不会有大问题,只不过同步提交时,如果有未完成的异步 changeset 会造成在主线程中进行布局计算,此时有可能会造成 UI 卡顿.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment