You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SwiftUI中收集KMP项目里的AndroidX Paging Flow<PagingData>?

在SwiftUI中处理KMP的Flow

要在SwiftUI中收集KMP返回的Flow<PagingData>,需要结合Kotlin协程的Swift适配库和Combine框架转换数据流,同时处理Paging3的分页数据结构,以下是具体实现步骤:

1. 确认KMP项目依赖配置

在commonMain的构建脚本中确保已引入Paging3和协程核心依赖:

val commonMain by getting {
    dependencies {
        implementation("androidx.paging:paging-common:3.2.1") // 使用最新稳定版
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
    }
}

iOS端需添加协程的Swift适配依赖,用于将Kotlin Flow转为Combine Publisher:

val iosMain by getting {
    dependencies {
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swift:1.7.3")
    }
}

2. Kotlin端封装数据流

在仓库类中保持原有返回Flow<PagingData<YourModel>>的方法,确保YourModel是KMP可跨平台访问的类型(比如用@Serializable标记序列化):

import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow

class YourRepository {
    fun getPagingData(): Flow<PagingData<YourModel>> {
        // 你的原有分页数据实现逻辑
    }
}

3. Swift端转换并处理数据流

3.1 导入依赖框架

import shared // 替换为你的KMP模块名称
import Combine
import SwiftUI

3.2 扩展Flow转Combine Publisher

利用kotlinx-coroutines-swift提供的能力,将Kotlin Flow转为Swift可订阅的Publisher:

extension Flow {
    func toPublisher() -> AnyPublisher<Output, KotlinError> {
        Publishers.KotlinFlow(flow: self)
            .eraseToAnyPublisher()
    }
}

3.3 ViewModel中处理分页数据

在ObservableObject的ViewModel中订阅数据流,解析PagingData的快照内容并更新UI状态:

class PagingViewModel: ObservableObject {
    @Published var listItems: [YourModel] = []
    @Published var isLoadingMore = false
    private var cancellables = Set<AnyCancellable>()

    init() {
        loadPagingData()
    }

    private func loadPagingData() {
        let repo = YourRepository()
        repo.getPagingData()
            .toPublisher()
            .receive(on: DispatchQueue.main)
            .sink(
                receiveCompletion: { [weak self] completion in
                    guard let self = self else { return }
                    self.isLoadingMore = false
                    if case .failure(let error) = completion {
                        print("分页加载失败: \(error.localizedDescription)")
                    }
                },
                receiveValue: { [weak self] pagingData in
                    guard let self = self else { return }
                    // 获取当前分页快照,提取已加载列表项
                    let snapshot = pagingData.snapshot()
                    self.listItems.append(contentsOf: snapshot.items)
                    // 监听追加加载状态
                    self.isLoadingMore = snapshot.loadState.append == .loading
                }
            )
            .store(in: &cancellables)
    }
}

3.4 SwiftUI视图展示数据

struct PagingListView: View {
    @StateObject private var viewModel = PagingViewModel()

    var body: some View {
        List(viewModel.listItems) { item in
            Text(item.title) // 替换为你的模型实际属性
        }
        .overlay(alignment: .bottom) {
            if viewModel.isLoadingMore {
                ProgressView()
                    .padding()
            }
        }
    }
}

关键说明

  • PagingData.snapshot()是核心方法,用于获取当前分页的所有已加载项和加载状态。
  • 如需支持下拉刷新,可在Kotlin仓库中添加刷新方法,Swift端调用后重新订阅数据流或触发分页刷新。
  • 确保YourModel在KMP中被正确暴露给iOS,避免使用iOS不支持的Kotlin语法特性。

内容的提问来源于stack exchange,提问作者KolaYAndr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 13:17:41