Flutter 并发性(针对 Swift 开发者)
Dart 和 Swift 都支持并发编程。本指南应能帮助你理解 Dart 中的并发性如何工作,以及它与 Swift 的比较。有了这种理解,你就能创建高性能的 iOS 应用。
在 Apple 生态系统中开发时,某些任务可能需要很长时间才能完成。这些任务包括获取或处理大量数据。iOS 开发者通常使用 Grand Central Dispatch (GCD) 来使用共享线程池调度任务。使用 GCD,开发者将任务添加到调度队列,然后 GCD 决定在哪个线程上执行它们。
但是,GCD 会启动线程来处理剩余的工作项。这意味着你最终可能会有大量的线程,并且系统可能会变得过载。使用 Swift,结构化并发模型减少了线程数量和上下文切换。现在,每个核心只有一个线程。
Dart 具有单线程执行模型,并支持 Isolate
、事件循环和异步代码。一个 Isolate
是 Dart 对轻量级线程的实现。除非你生成一个 Isolate
,否则你的 Dart 代码会在由事件循环驱动的主 UI 线程中运行。Flutter 的事件循环相当于 iOS 主循环——换句话说,是附加到主线程的 Looper。
Dart 的单线程模型并不意味着你需要将所有内容作为阻塞操作运行,从而导致 UI 冻结。相反,请使用 Dart 语言提供的异步特性,例如 async
/await
。
异步编程
#异步操作允许在它完成之前执行其他操作。Dart 和 Swift 都使用 async
和 await
关键字支持异步函数。在这两种情况下,async
标记一个函数执行异步工作,await
告诉系统等待函数的结果。这意味着 Dart VM 可以 在必要时暂停函数。有关异步编程的更多详细信息,请查看 Dart 中的并发性。
利用主线程/Isolate
#对于 Apple 操作系统,主线程(也称为主要线程)是应用程序开始运行的地方。用户界面的渲染始终发生在主线程上。Swift 和 Dart 的一个区别是
Swift 可能会为不同的任务使用不同的线程,并且 Swift 不保证使用哪个线程。因此,在 Swift 中调度 UI 更新时,你可能需要确保工作发生在主线程上。
假设你想编写一个异步获取天气并显示结果的函数。
在 GCD 中,要手动将进程调度到主线程,你可能会执行以下操作。
首先,定义 Weather
enum
enum Weather: String {
case rainy, sunny
}
接下来,定义视图模型并将其标记为 @Observable
,它发布类型为 Weather?
的 result
。使用 GCD 创建一个后台 DispatchQueue
将工作发送到线程池,然后调度回主线程以更新 result
。
@Observable class ContentViewModel {
private(set) var result: Weather?
private let queue = DispatchQueue(label: "weather_io_queue")
func load() {
// Mimic 1 second network delay.
queue.asyncAfter(deadline: .now() + 1) { [weak self] in
DispatchQueue.main.async {
self?.result = .sunny
}
}
}
}
最后,显示结果
struct ContentView: View {
@State var viewModel = ContentViewModel()
var body: some View {
Text(viewModel.result?.rawValue ?? "Loading...")
.onAppear {
viewModel.load()
}
}
}
最近,Swift 引入了 actor 以支持共享可变状态的同步。为了确保工作在主线程上执行,定义一个标记为 @MainActor
的视图模型类,其中包含一个使用 Task
内部调用异步函数的 load()
函数。
@MainActor @Observable class ContentViewModel {
private(set) var result: Weather?
func load() async {
// Mimic 1 second network delay.
try? await Task.sleep(nanoseconds: 1_000_000_000)
self.result = .sunny
}
}
接下来,使用 @State
将视图模型定义为状态,其中包含一个可由视图模型调用的 load()
函数
struct ContentView: View {
@State var viewModel = ContentViewModel()
var body: some View {
Text(viewModel.result?.rawValue ?? "Loading...")
.task {
await viewModel.load()
}
}
}
在 Dart 中,默认情况下所有工作都在主 isolate 上运行。要在 Dart 中实现相同的示例,首先,创建 Weather
enum
enum Weather { rainy, windy, sunny }
然后,定义一个简单的视图模型(类似于在 SwiftUI 中创建的),以获取天气。在 Dart 中,Future
对象表示将来提供的值。Future
类似于 Swift 的 @Observable
。在此示例中,视图模型中的函数返回一个 Future<Weather>
对象
@immutable
class HomePageViewModel {
const HomePageViewModel();
Future<Weather> load() async {
await Future.delayed(const Duration(seconds: 1));
return Weather.sunny;
}
}
此示例中的 load()
函数与 Swift 代码有相似之处。Dart 函数被标记为 async
,因为它使用了 await
关键字。
此外,标记为 async
的 Dart 函数会自动返回一个 Future
。换句话说,你无需在标记为 async
的函数内部手动创建 Future
实例。
最后一步,显示天气值。在 Flutter 中,FutureBuilder
和 StreamBuilder
Widgets 用于在 UI 中显示 Future 的结果。以下示例使用 FutureBuilder
class HomePage extends StatelessWidget {
const HomePage({super.key});
final HomePageViewModel viewModel = const HomePageViewModel();
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
// Feed a FutureBuilder to your widget tree.
child: FutureBuilder<Weather>(
// Specify the Future that you want to track.
future: viewModel.load(),
builder: (context, snapshot) {
// A snapshot is of type `AsyncSnapshot` and contains the
// state of the Future. By looking if the snapshot contains
// an error or if the data is null, you can decide what to
// show to the user.
if (snapshot.hasData) {
return Center(child: Text(snapshot.data.toString()));
} else {
return const Center(child: CupertinoActivityIndicator());
}
},
),
);
}
}
有关完整示例,请查看 GitHub 上的 async_weather 文件。
利用后台线程/Isolate
#Flutter 应用可以在各种多核硬件上运行,包括运行 macOS 和 iOS 的设备。为了提高这些应用的性能,有时你必须在不同的核心上并发运行任务。这对于避免长时间运行的操作阻塞 UI 渲染尤其重要。
在 Swift 中,你可以利用 GCD 在具有不同服务质量 (qos) 属性的全局队列上运行任务。这表示任务的优先级。
func parse(string: String, completion: @escaping ([String:Any]) -> Void) {
// Mimic 1 sec delay.
DispatchQueue(label: "data_processing_queue", qos: .userInitiated)
.asyncAfter(deadline: .now() + 1) {
let result: [String:Any] = ["foo": 123]
completion(result)
}
}
}
在 Dart 中,你可以将计算卸载到工作隔离器 (worker isolate),通常称为后台 worker。一个常见的场景是生成一个简单的工作隔离器,并在 worker 退出时在消息中返回结果。从 Dart 2.19 开始,你可以使用 Isolate.run()
来生成一个隔离器并运行计算
void main() async {
// Read some data.
final jsonData = await Isolate.run(() => jsonDecode(jsonString) as Map<String, dynamic>);`
// Use that data.
print('Number of JSON keys: ${jsonData.length}');
}
在 Flutter 中,你还可以使用 compute
函数来启动一个隔离器以运行回调函数
final jsonData = await compute(getNumberOfKeys, jsonString);
在这种情况下,回调函数是如下所示的顶级函数
Map<String, dynamic> getNumberOfKeys(String jsonString) {
return jsonDecode(jsonString);
}
你可以在 Swift 开发者学习 Dart 中找到更多关于 Dart 的信息,在 适用于 SwiftUI 开发者的 Flutter 或 适用于 UIKit 开发者的 Flutter 中找到更多关于 Flutter 的信息。