在 KMM 中处理平台专有功能的最佳实践

发布于 2025-01-14 23:03:01 字数 606 浏览 1 评论 0原文

我有一个现有的 iOS 应用程序,我正在尝试 KMM。理想情况下,启动后,该应用程序将同时适用于 Android 和 iOS,但我希望现有的 iOS 用户不要丢失他们当前的内容。

我开始在 iosMain 中构建一个 MigratorService ,因为它要做的就是处理特定于平台的模型,但后来我没有办法从commonMain。所以我要么:

  • 将我在 iosMain 中完成的所有代码移动到 commonMain 。尽管存在将旧的 iOS 相关代码暴露给 Android 的潜在风险,但实际上这种情况永远不会发生,因为迁移仅发生在 iOS 上。
  • 在 commonMain 中创建一个 Expect MigratorService,实际实现在 iosMain 上。但这种方法还意味着在 androidMain 中添加完整的 no-op 版本的 MigratorService。这暴露了一个 API,但它并没有真正实现预期的功能。

哪一种被认为是 KMM 中的最佳实践?为什么?或者有其他替代方案来解决这些情况吗?

干杯

I have an existing iOS app and I'm trying out KMM. Ideally, when launched, the app would be available both for Android and iOS, but I'd like for existing iOS users not to lose their current content.

I started building a MigratorService in iosMain, as all it's going to be doing is dealing with platform-specific models, but then I didn't have a way to call it from commonMain. So I either:

  • Move all the code I've done in iosMain to commonMain. Although there is the potential risk of exposing old iOS related code to Android, in reality this would never happen since the migration only happens for iOS.
  • Create an expect MigratorService in commonMain, with the actual implementation being on iosMain. But this approach implies also adding a complete no-op version of MigratorService in androidMain. Which is exposing an API that doesn't really do what is expected.

Which one would be considered the best practice in KMM and why? Or is there another alternative to tackle these situations?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文