swift - Combine - Assigning publisher value
问题描述
I'm sorry if this is a basic question but I'm still getting to grips with Combine.
I have a publisher that is linked to changes in CoreData.
I am trying to subscribe to those changes in my ViewModel using :
CDPublisher(
request: Book.fetchAllBooks(),
context: PersistentStore.shared.context
)
.map { $0 }
.receive(on: DispatchQueue.main)
.sink { [weak self] newBooks in
self?.books = newBooks // Error here
}
.store(in: &cancellables)
I want to assign the new array of books newBooks
to an @Published
array called books
of type [Book]
.
However I get this error:
Cannot assign value of type 'Subscribers.Completion<CDPublisher.Failure>' (aka 'Subscribers.Completion') to type '[Book]'
I believe the problem stems from my .map
. I added it because without it I got this error:
Referencing instance method 'sink(receiveValue:)' on 'Publisher' requires the types 'CDPublisher.Failure' (aka 'Error') and 'Never' be equivalent
FYI the publisher I'm using is taken from: https://betterprogramming.pub/combine-publishers-and-core-data-424b68fe9473
Thanks for your help.
解决方案
推荐阅读
- list - ocaml 列表到数组数组
- android - 将 imagview 资源传递给意图
- .net - When using .Net asynchronous sockets, which thread are callbacks called from?
- c# - 我收到错误“EntityType has no key defined”,即使我在模型和数据库中都定义了一个键
- linux - 如何使用 SPI 编写网络设备驱动程序?
- java - counter for the number of responses for specific paramater of a hashmap JAVA enum
- python - Positioning colorbar and second y-axis (matplotlib)
- c# - 如何解决dotnet核心中的nuget依赖地狱?
- ruby-on-rails - Ruby - Selenium Webdriver - 没有这样的元素:无法找到元素:
- dart - Dart:如何忽略 omit_local_variable_types 警告?