I was trying to use dataTaskPublisher in my project.
I already use dataTask successfully.
This is the code that I use it, it works when I use dataTask, but in this case with dataTaskPublisher it didn't get response from the print statements. Why is this happening ?
var cancels: Set<AnyCancellable> = []
let request = URLSessionHelper.getRequestWithUrl(endpointUrl: url, httpMethod: HTTPMethod.get, jsonBodyData: nil)
let session = URLSession(configuration: URLSessionHelper.sessionConfiguration, delegate: SessionDelegate(), delegateQueue: nil)
session.dataTaskPublisher(for: request)
.map(\.data)
.compactMap { String(data: $0, encoding:. utf8) }
.sink { error in
print("error: \(error)")
// Cannot assign value of type 'Subscribers.Completion<Error>' to type 'Error?'
} receiveValue: { resutl in
print("resutl: \(resutl)")
}
.store(in: &cancels)
CodePudding user response:
session
is going immediately out-of-scope -- the Combine chain doesn't hold a strong reference to it.
You should store a reference to it as an instance variable on your class, which will prevent this problem.