Combine publisher .store(in: ) memory usage

I've enjoyed working with Combine in my first serious project with it, but I'm curious about the following scenario. Suppose I have an API manager class that the app can use to interact with an API. If I use .store(in: ) with a dataTaskPublisher, does Combine add a completion handler to remove that publisher when it completes? Currently I'll only have up to a few hundred interactions with the API so it doesn't seem to matter too much, but I'm curious if this is something I'll have to do differently in the future.

If .store(in:) doesn't clean up after itself, is there any way to reference the publisher in the .sink() completion handler? I know I could assign the publisher to a variable which I then reference in the completion handler, and then cancellables.append(thePublisher) but using .store(in:) is much cleaner.

class APIManager {
	static let shared = APIManager()
	private let baseURL = URL(string: "")!
	private var cancellables = Set<AnyCancellable>()
	private lazy var jsonDecoder : JSONDecoder = {
		let decoder = JSONDecoder()
		// set up decoder options for the API
		return decoder
	// MARK:- Get initial info from API
	func loadInfo() {
		guard let url = baseURL.appendingPathComponent("orders/1234.json") else { return }
		var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
		// add queryItems
		URLSession.shared.dataTaskPublisher(for: components.url!)
			.map { $ }
			.decode(type: APIInfoResult.self, decoder: self.jsonDecoder)
			.receive(on: DispatchQueue.main)
			.sink(receiveCompletion: { completion in
			}, receiveValue: { ordersInfo in
			.store(in: &cancellables)
	// MARK:- Update API
	// ...

Terms of Service

Privacy Policy

Cookie Policy