首页指南参考教程

带有 Expo 的本地优先架构

新兴的本地优先软件运动的介绍,包括相关学习资源和工具的链接。


本指南正在进行中。如果你有任何反馈,请访问我们 GitHub 存储库中的 React Navigation 中显示的选项文档

术语 "local-first" 最早是在由研究实验室 Ink & Switch 撰写的论文 "本地优先软件" 中提出的,但其背后的想法已经存在很长时间了。它是支持我们最喜欢的一些应用的架构,例如 LinearSuperhumanExcalidraw 甚至 Apple 说明

¥The term "local-first" was first coined in the paper "Local-first software", written by the research lab Ink & Switch, but the ideas behind it have been around for a long time. It's the architecture that powers some of our favorite apps, like Linear, Superhuman, Excalidraw, and even Apple Notes.

在本地优先软件中,"另一台计算机的可用性绝不会妨碍你工作"(在 YouTube 上观看)。离线时,你仍然可以直接从设备上的数据库读取和写入。你可以信任该软件离线工作,并且你知道当你连接到互联网时,你的数据将无缝同步并可在运行该应用的任何设备上使用。当你在线时,此架构非常适合 "multiplayer" 应用、由 Figma 推广

¥In local-first software, "the availability of another computer should never prevent you from working" (via Martin Kleppmann). When you are offline, you can still read and write directly from/to a database on your device. You can trust the software to work offline, and you know that when you are connected to the internet, your data will be seamlessly synced and available on any of your devices running the app. When you're online, this architecture is well suited for "multiplayer" apps, as popularized by Figma.

要深入了解本地优先是什么以及它是如何工作的,请参阅下面的 其他资源

¥To dig deeper into what local-first is and how it works, refer to the additional resources below.

为什么使用本地优先架构?

¥Why use local-first architecture?

用户体验优势

¥User experience benefits

本地优先软件感觉很快,因为交互不再受网络限制,你可以直接从设备上的数据库读取和写入。

¥Local-first software feels fast because interactions are no longer network-bound, you can read and write directly from/to a database on your device.

你可以信任该软件离线工作,并且你知道当你连接到互联网时,你的数据将无缝同步并可在运行该应用的任何设备上使用。

¥You can trust the software to work offline, and you know that when you are connected to the internet, your data will be seamlessly synced and available on any of your devices running the app.

本地优先软件的另一个特点是它具有协作性 - 多个设备可以处理相同的数据,并且更改会在所有设备上同步。当你在 菲格玛 中协作设计时,这可以实时发生,或者当你在 Linear 中离线创建任务时,这可以异步发生,当你再次在线时它会同步。

¥Another characteristic of local-first software is that it is collaborative — multiple devices can work on the same data, and changes are synced across all of them. This can happen in real-time when you collaborate on a design in Figma or asynchronously when you create a task while offline in Linear and it is synced when you are online again.

开发者体验优势

¥Developer experience benefits

你不再需要为每个网络请求管理应用的各种状态 — "loaded"、"loading"、"error" 等,以及它们对应的 UI 状态和其他逻辑。写入本地数据库,应用将自动将更改同步到服务器。这意味着你可以专注于构建应用,而不必担心网络和离线状态。

¥You no longer have to manage various states of your app for each network request — "loaded", "loading", "error", and so on, with their corresponding UI states and other logic. Write to a local database, and the app will automatically sync the changes to the server. This means that you can focus on building the app, and not worry as much about the networking and offline states.

你的服务器可用性可能仍然很重要,但如果发生中断,你的用户仍然可以访问应用并继续工作。你甚至可以提供一种无需通过服务器即可同步数据的机制。

¥Your server availability may still be important, but in the event of an outage your users can still access the app and continue working. You may even provide a mechanism to sync the data without going through your server.

构建本地优先应用的挑战

¥Challenges in building local-first apps

目前可用的工具仍处于早期阶段,因此你可能会发现自己正在解决你期望通过当前使用的工具解决的问题。例如,你可能需要实现自定义同步层,或者你可能必须弄清楚如何处理对同一数据进行操作的多个用户的权限。随着生态系统的发展,我们预计构建本地优先应用将变得更加容易。如果你不准备成为早期采用者,以及随之而来的一切,你可能需要等待工具成熟后再开始使用本地优先工具构建应用。

¥The tools available today are still in their early stages, and so you may find yourself solving problems that you would expect to be solved by the tools you are using today. For example, you may need to implement a custom sync layer, or you may have to figure out how to handle permissions for multiple users operating on the same data. As the ecosystem evolves, we expect it to become easier to build local-first apps. If you're not prepared to be an early adopter, and everything that comes with that, you might want to wait for the tools to mature before you start building your app with local-first tools.

用于构建本地优先应用的工具

¥Tools for building local-first apps

"本地优先软件" 社区网站 上提供了全面的工具列表。以下是我们在 Expo 有直接使用经验的工具的简短列表。

¥A comprehensive list of tools is available on the "Local-first software" community website. The following is a shorter list of tools that we at Expo have had direct experience working with.

考虑本地优先工具的一种方法是按以下类别对它们进行分组:持久性、状态管理和同步。如果某些工具可以处理问题的多个方面,它们将适合多个类别。同步可以进一步细分为可同步的数据结构和传输层。

¥One way to think of local-first tools is to group them by the following categories: persistence, state management, and syncing. Some tools will fit into multiple categories if they handle multiple aspects of the problem. Syncing can be further subdivided into syncable data structures and transport layers.

TinyBase

TinyBase 自称为 "本地优先应用的反应式数据存储"。它是一个状态管理库,可插入许多最流行的同步和持久层,例如 YjsSQLite。它是构建需要持久化和同步数据的本地优先应用的绝佳选择。使用 TinyBase 示例 开始:npx create-expo-app --example with-tinybase

¥TinyBase calls itself "the reactive data store for local-first apps". It is a state management library that plugs in to many of the most popular syncing and persistence layers, such as Yjs and SQLite. It's a great choice for building local-first apps that need to persist and sync data. Get started by using the TinyBase example: npx create-expo-app --example with-tinybase.

SQLite

Expo SQLite 是一个 SQLite 库,是本地优先应用持久性的绝佳选择。你可以在 SQLite 前面使用不同的状态管理和同步层,例如 y-expo-sqlite 用于持久化 Yjs 文档,TinyBase 作为状态管理层。使用 SQLite 非常灵活,但你需要将其与其他工具结合使用或构建自己的工具以获得完整的本地优先解决方案。请参阅 Expo SQLite API 参考 了解更多信息。

¥Expo SQLite is a SQLite library that is a great choice for persistence for local-first apps. You can use SQLite with different state management and syncing layers in front of it, such as y-expo-sqlite to persist Yjs documents, and TinyBase as a state management layer. Using SQLite is flexible, but you will need to combine it with other tools or build your own tools to get a complete local-first solution. See Expo SQLite API reference for more information.

Yjs

Yjs 是一个 CRDT 实现,它提供可以在多个客户端之间同步的数据类型。当使用 Yjs 构建应用并处理你希望能够同步的数据时,你将使用 Y.ArrayY.Map 来表示你的数据,而不是 ArrayObject。你可以在 Yjs 之上使用像 TinyBase 这样的库进行状态管理,并且持久性可以通过各种工具来处理,从文件系统上的 JSON 文件到成熟的数据库(例如 y-expo-sqlite)以及介于两者之间的所有内容。请参阅 Yjs 的 GitHub 存储库 了解更多信息。

¥Yjs is a CRDT implementation that provides data types that can be synced across multiple clients. When building an app with Yjs and working with data that you would like to be able to sync, then you would use Y.Array and Y.Map to represent your data rather than Array and Object. You may use a library like TinyBase for state management on top of Yjs, and persistence can be handled by a variety of tools, from a JSON file on your filesystem to a full-fledged database (such as y-expo-sqlite) and everything in between. See Yjs's GitHub repository for more information.

Prisma

Prisma 是众所周知的 Node.js 和 TypeScript 后端最流行的 ORM,现在可用于 Expo 和 React Native 早期访问。Prisma 旨在提供完整的本地优先解决方案,为你涵盖状态管理、同步和持久性。虽然还处于早期阶段,但 Beto Moedano 已经整理了使用 Prisma 和 Expo 构建本地优先 Notion 克隆、你的应用代码不是存储密钥的安全场所在 GitHub 上查看代码 的完整演练。

¥Prisma is well known as the most popular ORM for Node.js and TypeScript backends, and it's now available for Expo and React Native in early access. Prisma aims to provide a complete local-first solution, with state management, syncing, and persistence all covered for you. While it's still early, Beto Moedano has put together full walkthrough of using Prisma with Expo to build a local-first Notion clone, watch it on YouTube and check out the code on GitHub.

其他工具

¥Other tools

以下列表远非全面,它提供了引起我们注意的其他工具,你可能会觉得很有趣。有关更详细的工具列表,请参阅 "本地优先软件" 社区网站

¥The following list, far from being comprehensive, provides other tools that have caught our attention and that you may find interesting to explore. For a more thorough list of tools, see "Local-first software" community website.

  • LiveStore

  • 自动合并

    ¥Automerge

  • ElectricSQL

  • 即时

    ¥Instant

  • PowerSync

其他资源

¥Additional resources

  • Martin Kleppmann 的 "本地优先的过去、现在和未来"

    ¥"The past, present, and future of local-first" by Martin Kleppmann

  • Ink & Switch 的 "本地优先软件"

    ¥"Local-first software" by Ink & Switch

  • "本地优先软件" 社区网站最低 Xcode 版本要求

    ¥"Local-first software" community website and meetup playlist on YouTube

  • Johannes Schickling 的 YouTube 上的聚会播放列表

    ¥localfirst.fm podcast by Johannes Schickling

Expo 中文网 - 粤ICP备13048890号