Customize handling of asynchronous events by combining event-processing operators using Combine.

Combine Documentation

Posts under Combine tag

26 Posts
Sort by:
Post not yet marked as solved
0 Replies
659 Views
Can someone please help me understand PassthroughSubject and CurrentValueSubject? What I understand so far is that they are subjects where subscribers can listen to changes made to these subjects, but I'm really straggling to understand the following. I'm I correct by saying that PassthroughSubject or CurrentValueSubject could replace delegation and asynchronous function calls? Is it possible to delare a subject in Class A and subscribe to listen to those subject changes in Class B and in some other classes or are listeners meant to only be used direclty in SwiftUI structs? Thanks
Posted
by fsdolphin.
Last updated
.
Post not yet marked as solved
1 Replies
1.1k Views
I'm trying to get push tokens for Live Activities but I am getting the same token twice, Why is that? Is there any other way to get the token just once, so I can hit the API to save the token. Here's the code: Task { for await data in runningActivity.pushTokenUpdates { let myToken = data.hexString self.count += 1 print("Count \(self.count)\n" + myToken) } Output: Count 1 80dc21086f81.........646d7084805dc Count 2 80dc21086f81.........646d7084805dc I can't seem to understand why this is happening, and some times it takes significantly longer to get the tokens. Am I doing anything wrong? Please do share your thoughts. Thank you!
Posted Last updated
.
Post not yet marked as solved
0 Replies
509 Views
What is the expected thread on which a .collect operation in Swift's Combine will emit? Specifically I am seeing this code crash in the second precondition, but not the first: return Publishers.MergeMany(publishers) .handleEvents(receiveOutput: { _ in precondition(Thread.isMainThread) // <- This is fine }) .collect() .handleEvents(receiveOutput: { _ in precondition(Thread.isMainThread) // <- Crash is here }) It is as if the .collect operation is picking a different thread to use, even though the final publisher in the MergeMany must have emitted on the main thread. I have deduced this behavior via crash logs uploaded from Firebase. Can anyone explain this observed behavior?
Posted
by es1997.
Last updated
.
Post not yet marked as solved
0 Replies
688 Views
I've been struggling with writing some dependency injection types for Foundation types like NotificationCenter, UIDevice, etc. I've created a sample package to demonstrate: https://github.com/MFB-Technologies-Inc/notification-center-client-demo. The goal is to mimic NotificationCenter's Publisher and notifications AsyncSequence. LiveNotificationCenterClientTests.testStream will fail unless it's run in isolation. If more than one test is run, it will never complete because no values are ever received from the stream. MockNotificationCenterClientTests.testPublisher fails because the expectations are not fulfilled before the timeout. For whatever reason, the published values are not being received in the sink. MockNotificationCenterClientTests.testStream never completes because the value is being endlessly awaited. It seems that there are some fundamental things about concurrency that I'm getting wrong. Can anybody help me debug this? For the two stream tests, I know I could rewrite them to fail with a timeout like the publisher tests but that's not the real problem.
Posted Last updated
.
Post not yet marked as solved
1 Replies
702 Views
Hello, I've completed the Landmarks App Apple Developer tutorial, since finishing it I decided to create a project using the .JSON code from the project. I've wrote the code in an identical layout as the tutorial, however I am receiving the error - Couldn't parse lesssonData.json as array - when trying to load a preview and the app builds successfully but crashes when I launch it. Below is the code, any help is appreciated! LessonList.swift import SwiftUI struct LessonList: View { var lesson: Lesson var body: some View { VStack { List { Section { ForEach(lessons, id: \.self) { lesson in Label(lesson.name, systemImage: "house") } } Section { Label("Hello World!", systemImage: "globe") } } } } } struct LessonList_Previews: PreviewProvider { static var lessons = ModelData().lessons static var previews: some View { LessonList(lesson: lessons[0]) } } ModelData.swift import Foundation import Combine final class ModelData: ObservableObject { @Published var lessons: [Lesson] = load("lessonData.json") } var lessons: [Lesson] = load("lessonData.json") func load<T: Decodable>(_ filename: String) -> T { let data: Data guard let file = Bundle.main.url(forResource: filename, withExtension: nil) else { fatalError("Couldn't find \(filename) in main bundle.") } do { data = try Data(contentsOf: file) } catch { fatalError("Couldn't load \(filename) from main bundle:\n\(error)") } do { let decoder = JSONDecoder() return try decoder.decode(T.self, from: data) } catch { fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)") // Error on this Line } } lessonData.JSON [ { "id":"1001", "lessonNo":"Lesson 1", "name":"Introduction to Photography", "category":"Introduction", }, { "id":"1002", "lessonNo":"Lesson 2", "name":"Negative and Positive Space", "category":"Introduction", }, { "id":"1003", "lessonNo":"Lesson 3", "name":"Introduction to Camera Angles", "category":"Introduction", }, { "id":"1004", "lessonNo":"Lesson 4", "name":"Lighting and Framing", "category":"Beginners", }, { "id":"1005", "lessonNo":"Lesson 5", "name":"Still Photography", "category":"Beginners", }, { "id":"1006", "lessonNo":"Lesson 6", "name":"Motion Photograhy", "category":"Beginners", }, { "id":"1007", "lessonNo":"Lesson 7", "name":"Aperture and F-Stops", "category":"Intermiediate", }, { "id":"1008", "lessonNo":"Lesson 8", "name":"Shutter Speeds", "category":"Intermiediate", }, { "id":"1009", "lessonNo":"Lesson 9", "name":"Advanced Framing", "category":"Advanced", }, { "id":"1010", "lessonNo":"Lesson 10", "name":"Advanced Aperture, F-Stops and Shutter Speeds", "category":"Advanced", }, ]
Posted
by E-K.
Last updated
.
Post not yet marked as solved
1 Replies
634 Views
I'm converting some Combine publishers to async using .values and trying to understand what happens when: A Publisher completes before emitting a value. Here is some example code: let response = client.fetch(query: query) .tryMap { /* map the data, or throw a mapping error if it doesn't map. */ } return Model() } .eraseToAnyPublisher() .values for try await result in response { return result } throw AsyncCombineError.finishedWithoutValue // my custom error What happens with .values if the publisher completes without emitting any values? It returns a type of AsyncThrowingSequence<Self> Will it throw? return? hang? or is this state impossible to happen? Thanks and please enlighten me if I am misunderstanding structured concurrency. This is new to me!
Posted Last updated
.