Hi,
When I try to get data from server with login credential and it doesn't work as expected. Please see my code beblow:import SwiftUI
import Combine
struct Response: Codable {
var results: [Result]
}
struct Result: Hashable, Codable {
var trackId: String
var friendId: String
var trackName: String
var trackUrl: String
var status: String
}
class blacklistViewModel: ObservableObject {
@Published var results = [Result]()
@KeyChain(key: "zid" ,account: "zLogin") var storedId
@KeyChain(key: "zpassword",account: "zLogin") var storedPassword
var cancellables = Set<AnyCancellable>()
init() {
fetch()
}
func fetch(){
guard let url = URL(string: appUrl + "[login to view URL]") else { return }
let parameters = ["id" : String(data: [login to view URL]!, encoding: .utf8)!, "password": String(data: [login to view URL]!, encoding: .utf8)!]
var request = URLRequest(url:url)
[login to view URL] = "POST"
[login to view URL]("application/json;charset=utf-8", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? [login to view URL](withJSONObject: parameters, options: []) else { return }
[login to view URL] = httpBody
[login to view URL](for: request)
.subscribe(on: [login to view URL](qos: .background))
.receive(on: [login to view URL])
.tryMap(handleOutput)
.decode(type: [Result].self, decoder: JSONDecoder())
.sink{(completion) in
switch completion {
case .finished:
break
case .failure(_):
break
}
} receiveValue: { [weak self] (returnedPhotoModels) in
print(returnedPhotoModels)
self?.results = returnedPhotoModels
}.store(in: &cancellables)
}
private func handleOutput(output: [login to view URL]) throws -> Data {
guard let response = [login to view URL] as? HTTPURLResponse, [login to view URL] >= 200 && [login to view URL] < 300 else {
throw URLError(.badServerResponse)
}
return [login to view URL]
}
}
Please help me to fix it. I really need to use Combine for this