Skip to content

Create Custom Publisher & Subscriber & Subscription with iOS Combine Framework

License

Notifications You must be signed in to change notification settings

XingZhaoDev/CustomDataTaskWithCombine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CustomDataTaskWithCombine

Create Custom Data Task (URLSession)Publisher & Subscriber & Subscription with iOS Combine Framework. This project can help developers to better understand the Combine Framework.

class CustomDataTaskSubscriber<Input: Decodable>: Subscriber {

typealias Failure = Error

//Subscriber has to retain the subscription
var subscription: Subscription?

func receive(subscription: Subscription) {
    print("Received subscription")
    self.subscription = subscription
    
    // subscriber ask data from subscription. .unlimited means the subscriber will receive data as much as possible
    subscription.request(.unlimited)
}

func receive(_ input: Input) -> Subscribers.Demand {
    print("Received input \(input)")
    return .none
}

func receive(completion: Subscribers.Completion<Error>) {
    print("Received completion \(completion)")
    cancel()
}

extension URLSession {
struct CustomDataTaskPubliser<Output: Decodable>: Publisher {
    
    
    typealias Failure = Error
    let urlRequest: URLRequest

    func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {
        let subscription = CustomDataTaskSubscription(urlRequest: urlRequest, subscriber: subscriber)
        subscriber.receive(subscription: subscription)
    }
}

func customDtaTaskPublisher<Output: Decodable>(for urlRequest: URLRequest) -> CustomDataTaskPubliser<Output> {
    return CustomDataTaskPubliser<Output>(urlRequest: urlRequest)
}

}

About

Create Custom Publisher & Subscriber & Subscription with iOS Combine Framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages