Swift Combine — Notification、URLSession、Timer等Publisher的理解与使用

Notification Publisher

SwiftCombine框架中,可以使用NotificationCenter.Publisher来创建一个能够订阅和接收通知的Publisher

// 创建一个订阅通知的Publisher
let notificationPublisher = NotificationCenter.default.publisher(for: Notification.Name("CustomNotification"))

接下来,我们可以订阅这个Publisher,并处理接收到的通知。

// 订阅通知
let cancellable = notificationPublisher.sink { notification in
    // 处理接收到的通知
    print("Received notification: \(notification)")
}

发送通知

// 发送通知
NotificationCenter.default.post(name: Notification.Name("CustomNotification"), object: nil)

下面代码中就是一个完整的例子:

class NotificationViewModel: ObservableObject {
  private var cancellable = Set<AnyCancellable>()

  func setUpNotification() {
    let notificationPublisher = NotificationCenter.default.publisher(for: Notification.Name("CustomNotification"))
    notificationPublisher
      .sink { notification in
        print("Received notification: \(notification)")
      }
      .store(in: &cancellable)
  }

  func sendNotification() {
    NotificationCenter.default.post(name: Notification.Name("CustomNotification"), object: nil)
  }
}

struct NotificationDemo: View {
  @StateObject private var viewModel = NotificationViewModel()

  var body: some View {
    Button("Send Notification") {
      viewModel.sendNotification()
    }
    .buttonStyle(BorderedProminentButtonStyle())
    .onAppear {
      viewModel.setUpNotification()
    }
  }
}

上面代码中在ViewModel中定义并且订阅了Notification Publisher,在SwiftUI界面触发NotificationCenter发送通知,随后在sink方法中收到了该通知。
在这里插入图片描述
除了这种用法外,有的时候也可以直接在SwiftUI界面通过onReceive方式使用。
现在SwiftUI界面定义一个Notification

// app 进入前台前的通知
let willEnterForegroundPublisher = NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)

然后设置onReceive方法:

.onReceive(willEnterForegroundPublisher, perform: { notification in
  print("Received App will enter foreground notification")
})

这样在App从后台回前台的时候就触发了这个通知,onReceive的闭包中的打印就会输出。

完整代码如下:

struct NotificationDemo1: View {
  // app回前台的通知
  let willEnterForegroundPublisher = NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)

  var body: some View {
    VStack {
      Text("Hello World")
    }
    .onReceive(willEnterForegroundPublisher, perform: { notification in
      print("Received App will enter foreground notification")
    })
  }
}

如果想在这个界面添加多个通知,那是不是要加多个onReceive方法呢?也可以不是的,比如像这样:

.onReceive(Publishers.MergeMany(willEnterForegroundPublisher, didEnterBackgroundPublisher), perform: { notification in
  print("Received App \(notification)")
})

可以通过Publishers.MergeMany方法将多个Publisher合并,然后在一个回调中处理收到通知事件。

struct NotificationDemo1: View {
  // app回前台的通知
  let willEnterForegroundPublisher = NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)

  // app进入后台通知
  let didEnterBackgroundPublisher = NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)

  var body: some View {
    VStack {
      Text("Hello World")
    }
    .onReceive(Publishers.MergeMany(willEnterForegroundPublisher, didEnterBackgroundPublisher), perform: { notification in
      print("Received App \(notification)")
    })
  }
}

在这里插入图片描述

URLSession Publisher

SwiftCombine框架中,URLSession.DataTaskPublisher提供了一种方便的方式来执行网络请求并处理返回的数据。

首先创建一个Publisher

// 创建一个网络请求Publisher
let url = URL(string: "https://......")!
let request = URLRequest(url: url)
let dataTaskPublisher = URLSession.shared.dataTaskPublisher(for: request)

接下来,我们可以订阅这个Publisher,并处理接收到的数据和错误。

// 订阅网络请求
let cancellable = dataTaskPublisher
    .map(\.data) // 提取返回的数据
    .decode(type: MyResponse.self, decoder: JSONDecoder()) // 解码数据为自定义类型
    .receive(on: DispatchQueue.main) // 切换到主线程处理结果
    .sink(receiveCompletion: { completion in
        switch completion {
        case .finished:
            print("Request completed successfully")
        case .failure(let error):
            print("Request failed with error: \(error)")
        }
    }, receiveValue: { response in
        print("Received response: \(response)")
    })

dataTaskPublisher 发送一个新的事件值时,我们将其中的 Data 通过 map 的方式提取出来,并交给 decode 这个 Operator 进行处理。decode 要求上游 PublisherOutput 类型是 Data,它会使用参数中接受的 decoder (本例中是 MyResponse) 来对上游数据进行解析,生成对应类型的实例,并作为新的 Publisher 事件发布出去。然后切换到主线程处理结果,包括刷新UI等等。

把上面的代码优化一下,具体化一下,实现一个真实的网络请求示例:

import SwiftUI
import Combine
import Foundation

struct Photo: Identifiable, Decodable {
  let id: Int
  let albumId: Int
  let title: String
  let url: String
  let thumbnailUrl: String
}

class URLSessionViewModel: ObservableObject {
  private var cancellable = Set<AnyCancellable>()
  @Published var photos: [Photo] = []
  @Published var isFetching: Bool = false

  func fetchPhotoData() {
    guard let url = URL(string: "https://jsonplaceholder.typicode.com/photos") else {
      return
    }
    isFetching = true
    let request = URLRequest(url: url)
    URLSession.shared.dataTaskPublisher(for: request)
      .map(\.data)
      .decode(type: [Photo].self, decoder: JSONDecoder())
      .receive(on: DispatchQueue.main)
      .sink { completion in
        switch completion {
          case .finished:
            print("Request completed successfully")
          case .failure(let error):
            print("Request failed with error: \(error)")
          }
      } receiveValue: { photos in
        print("Received response: \(photos)")
        self.isFetching = false
        self.photos = photos
      }
      .store(in: &cancellable)
  }
}

struct URLSessionDemo: View {
  @StateObject private var viewModel = URLSessionViewModel()

  var body: some View {
    VStack {
      if viewModel.photos.isEmpty {
        if viewModel.isFetching {
          ProgressView()
        } else {
          Button("Fetch photos data") {
            viewModel.fetchPhotoData()
          }
          .buttonStyle(BorderedProminentButtonStyle())
        }
      } else {
        List(viewModel.photos) { photo in
          PhotoView(photo: photo)
        }
        .listStyle(PlainListStyle())
      }
    }
  }
}

struct PhotoView: View {
  let photo: Photo

  var body: some View {
    HStack(spacing: 16) {
      AsyncImage(url: URL(string: photo.thumbnailUrl)) { image in
        image
          .resizable()
          .aspectRatio(contentMode: .fill)
      } placeholder: {
        Rectangle()
          .fill(Color.gray.opacity(0.3))
      }
      .frame(width: 80, height: 80)

      VStack {
        Text(String(photo.id))
          .font(.title)
          .frame(maxWidth: .infinity, alignment: .leading)

        Text(photo.title)
          .font(.headline)
          .frame(maxWidth: .infinity, alignment: .leading)
          .multilineTextAlignment(.leading)
          .lineLimit(2)
      }
    }
  }
}

上面代码中定义了一个Photo类型的数据,代码中采用了URLSession Publisher的方式请求数据,并在SwiftUI上显示,效果如下:
在这里插入图片描述

Timer Publisher

Timer 类型也提供了一个方法,来创建一个按照一定间隔发送事件的 Publisher。之前有一篇文章已经详细介绍过了,详见:SwiftUI中结合使用Timer和onReceive

写在最后

本文主要介绍了CombineNotificationURLSession Publisher的使用,尤其是配合SwiftUI界面的使用。不管是Notification还是URLSession都大大简化了代码,将代码流程集中化,实现了链式处理方式。

最后,希望能够帮助到有需要的朋友,如果觉得有帮助,还望点个赞,添加个关注,笔者也会不断地努力,写出更多更好用的文章。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/730268.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【阿里云服务器】【弹性云服务ECS】通过ssh登录远程服务器

一、操作系统 使用Windows11主机上的Ubuntu子系统&#xff0c;如下图所示&#xff1a; 二、云服务器登录方法 需知道&#xff1a;服务器ip地址、登录名和自己设置的登录密码&#xff1a; 上述系统用户名为root&#xff0c;需要在Ubuntu子系统中同样切换至root用户&#xff…

界面控件DevExpress v24.1全新发布 - 跨平台性进一步增强

DevExpress拥有.NET开发需要的所有平台控件&#xff0c;包含600多个UI控件、报表平台、DevExpress Dashboard eXpressApp 框架、适用于 Visual Studio的CodeRush等一系列辅助工具。屡获大奖的软件开发平台DevExpress 今年第一个重要版本v23.1正式发布&#xff0c;该版本拥有众多…

电子杂志制作工具推荐:让你轻松成为编辑大人

在这个数字化的时代&#xff0c;电子杂志已经成为信息传播的重要载体。它不仅能够满足人们对阅读的需求&#xff0c;还能够提供更加丰富、互动的阅读体验。因此&#xff0c;掌握一款好用的电子杂志制作工具&#xff0c;已经成为每个编辑大人的必备技能。接下来告诉大家一个超简…

Nacos安装教程(很细很简单),解决启动报错Please set the JAVA_HOME

nacos安装 找到你要下载的版本解压到任意非中文目录下端口默认8848&#xff0c;如有端口冲突&#xff0c;可修改配置文件中的端口。编辑shutdown.cmd文件&#xff0c;路径换成你的jdk安装地址否则会报错Please set the JAVA_HOME variable in your environment, We need java(x…

基于Pytorch框架的深度学习Vision Transformer神经网络蝴蝶分类识别系统源码

第一步&#xff1a;准备数据 6种蝴蝶数据&#xff1a;self.class_indict ["曙凤蝶", "麝凤蝶", "多姿麝凤蝶", "旖凤蝶", "红珠凤蝶", "热斑凤蝶"]&#xff0c;总共有900张图片&#xff0c;每个文件夹单独放一种…

使用自签名 TLS 将 Dremio 连接到 MinIO

Dremio 是一个开源的分布式分析引擎&#xff0c;为数据探索、转换和协作提供简单的自助服务界面。Dremio 的架构建立在 Apache Arrow&#xff08;一种高性能列式内存格式&#xff09;之上&#xff0c;并利用 Parquet 文件格式实现高效存储。有关 Dremio 的更多信息&#xff0c;…

跑通并使用Yolo v5的源代码并进行训练—目标检测

跑通并使用Yolo v5的源代码并进行训练 摘要&#xff1a;yolo作为目标检测计算机视觉领域的核心网络模型&#xff0c;虽然到24年已经出到了v10的版本&#xff0c;但也很有必要对之前的核心版本v5版本进行进一步的学习。在学习yolo v5的时候因为缺少论文所以要从源代码入手来体验…

Eureka 学习笔记(1)

一 、contextInitialized() eureka-core里面&#xff0c;监听器的执行初始化的方法&#xff0c;是contextInitialized()方法&#xff0c;这个方法就是整个eureka-server启动初始化的一个入口。 Overridepublic void contextInitialized(ServletContextEvent event) {try {init…

生产实习Day9 ---- Scala介绍

文章目录 Scala&#xff1a;融合面向对象与函数式编程的强大语言引言Scala与Java的互操作性Scala在大数据处理中的应用Scala的并发编程Scala的学习资源和社区结论 Scala&#xff1a;融合面向对象与函数式编程的强大语言 引言 Scala&#xff0c;全称Scalable Language&#xff…

教你开发一个适合外贸的消息群发工具!

在全球化日益加速的今天&#xff0c;外贸业务已经成为许多企业不可或缺的一部分&#xff0c;而在外贸业务中&#xff0c;高效的消息群发工具则扮演着至关重要的角色。 它能够帮助企业快速、准确地传达产品信息、促销活动等重要内容&#xff0c;从而提升业务效率和客户满意度&a…

项目经验——交通行业数据可视化大屏、HMI设计

交通行业数据大屏、HMI设计时要的注意点&#xff1a;清晰可读、简洁直观、适配性强。颜色对比度满足WCAG标准&#xff0c;深色背景减少干扰&#xff0c;实时展示交通数据&#xff0c;支持有线网络控制内容更新&#xff0c;保障驾驶安全与决策效率。

Linux企业 集群批量管理-秘钥认证

集群批量管理-秘钥认证 概述 管理更加轻松&#xff1a;两个节点&#xff0c;通过秘钥认证形成进行访问&#xff0c;不需要输入密码&#xff0c;单向服务要求&#xff08;应用场景&#xff09;&#xff1a; 一些服务在使用前要求我们做秘钥认证 手动写批量管理脚本名字&#x…

A800显卡驱动安装(使用deb安装)

重新安装显卡驱动&#xff0c;查阅了资料将过程记录如下&#xff1a; 1.下载deb安装包 打开nvidia官网查找对应的驱动版本&#xff0c;A800所在的选项卡位置如图&#xff1a; 点击查找后下载得到的是nvidia-driver-local-repo-ubuntu2004-550.90.07_1.0-1_amd64.deb安装包 2.…

猫头虎分享:IPython的使用技巧整理

&#x1f42f; 猫头虎分享&#xff1a;IPython的使用技巧整理 关于猫头虎 大家好&#xff0c;我是猫头虎&#xff0c;别名猫头虎博主&#xff0c;擅长的技术领域包括云原生、前端、后端、运维和AI。我的博客主要分享技术教程、bug解决思路、开发工具教程、前沿科技资讯、产品…

【学一点儿前端】单页面点击前进或后退按钮导致的内存泄露问题(history.listen监听器清除)

今天测试分配了一个比较奇怪的问题&#xff0c;在单页面应用中&#xff0c;反复点击“上一步”和“下一步”按钮时&#xff0c;界面表现出逐渐变得卡顿。为分析这一问题&#xff0c;我用Chrome的性能监控工具进行了浏览器性能录制。结果显示&#xff0c;每次点击“上一步”按钮…

【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 任务安排问题(200分) - 三语言AC题解(Python/Java/Cpp)

🍭 大家好这里是清隆学长 ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解 💻 ACM银牌🥈| 多次AK大厂笔试 | 编程一对一辅导 👏 感谢大家的订阅➕ 和 喜欢💗 📎在线评测链接 https://app5938.acapp.acwing.com.cn/contest/2/problem/OD…

cron.timezone

系统 date 数据库 show timezone插件 show cron.timezonealter system set cron.timezonePRC;show cron.timezone

前端新手小白的Vue3入坑指南

昨天有同学说想暑假在家学一学Vue3&#xff0c;问我有没有什么好的文档&#xff0c;我给他找了一些&#xff0c;然后顺带着&#xff0c;自己也写一篇吧&#xff0c;希望可以给新手小白们一些指引&#xff0c;Vue3欢迎你。 目录 1 项目安装 1.1 初始化项目 1.2 安装初始化依…

CDGA|数据治理要点是数据稳定、规范、安全,就像盖楼盘一样

在数字化浪潮汹涌的时代&#xff0c;数据已经成为企业运营和社会发展的核心驱动力。如同高楼大厦需要稳固的地基和规范的施工流程&#xff0c;数据治理同样需要确保数据的稳定性、规范性和安全性&#xff0c;以构建坚实可靠的数据大厦。 数据治理的首要任务是确保数据的稳定性 …

一文读懂过零检测电路的作用、电路原理图及应用

过零检测电路是一种常见的应用&#xff0c;其中运算放大器用作比较器。它通常用于跟踪正弦波形的变化&#xff0c;例如过零电压从正到负或从负到正。它还可以用作方波发生器。过零检测电路有许多应用&#xff0c;例如标记信号发生器、相位计和频率计。#过零检测电路#可以采用多…