无法使用SwiftUI中的Combine框架从Spotify API中获取数据

时间:2020-10-22 20:19:56

标签: api swiftui spotify combine

这是我的想法,我想在用户通过$artistName搜索时显示数据,然后合并框架可以帮助我从服务器请求数据。

我不知道我错了哪一步。当我尝试通过模拟器获取数据时。它将显示未知错误以及其余错误。

2020-10-23 03:56:38.220523+0800 PodcastSearchV2[42967:667554] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
Fetch failed: Unknown error
2020-10-23 03:56:40.114906+0800 PodcastSearchV2[42967:667546] [connection] nw_connection_copy_protocol_metadata [C2] Client called nw_connection_copy_protocol_metadata on unconnected nw_connection
2020-10-23 03:56:40.115071+0800 PodcastSearchV2[42967:667546] [connection] nw_connection_copy_protocol_metadata [C2] Client called nw_connection_copy_protocol_metadata on unconnected nw_connection
2020-10-23 03:56:40.115255+0800 PodcastSearchV2[42967:667546] [connection] nw_connection_copy_protocol_metadata [C2] Client called nw_connection_copy_protocol_metadata on unconnected nw_connection
2020-10-23 03:56:40.115360+0800 PodcastSearchV2[42967:667546] [connection] nw_connection_copy_protocol_metadata [C2] Client called nw_connection_copy_protocol_metadata on unconnected nw_connection
2020-10-23 03:56:40.115516+0800 PodcastSearchV2[42967:667546] [connection] nw_connection_copy_connected_local_endpoint [C2] Client called nw_connection_copy_connected_local_endpoint on unconnected nw_connection
2020-10-23 03:56:40.115613+0800 PodcastSearchV2[42967:667546] [connection] nw_connection_copy_connected_remote_endpoint [C2] Client called nw_connection_copy_connected_remote_endpoint on unconnected nw_connection
2020-10-23 03:56:40.115723+0800 PodcastSearchV2[42967:667546] [connection] nw_connection_copy_connected_path [C2] Client called nw_connection_copy_connected_path on unconnected nw_connection
2020-10-23 03:56:40.115924+0800 PodcastSearchV2[42967:667546] Connection 2: unable to determine interface type without an established connection
2020-10-23 03:56:40.116069+0800 PodcastSearchV2[42967:667546] Connection 2: unable to determine interface classification without an established connection
2020-10-23 03:56:40.116381+0800 PodcastSearchV2[42967:667546] [connection] nw_connection_copy_protocol_metadata [C2] Client called nw_connection_copy_protocol_metadata on unconnected nw_connection
2020-10-23 03:56:40.120441+0800 PodcastSearchV2[42967:667546] [connection] nw_connection_copy_metadata [C2] Client called nw_connection_copy_metadata on unconnected nw_connection
2020-10-23 03:56:40.120579+0800 PodcastSearchV2[42967:667546] Connection 2: unable to determine interface type without an established connection
2020-10-23 03:56:40.121823+0800 PodcastSearchV2[42967:667546] Connection 2: unable to determine interface type without an established connection

这是我的答复。

struct DataResponseSpotify: Codable {
    var episodes: PodcastItemSpotify
}

struct PodcastItemSpotify: Codable {
    var items: [PodcastItemsDetailsSpotify]
}

struct PodcastItemsDetailsSpotify: Codable, Identifiable {
    let id: String
    let description: String
    let images: [Images]
    let name: String
    let external: String
    
    var externalURL: URL? {
        return URL(string: external)
    }
    
    enum CodingKeys: String, CodingKey {
        case id
        case description
        case images
        case name
        case external = "external_urls"
    }
}

struct Images: Codable {
    let url: String
    
    var imageURL: URL? {
        return URL(string: url)
    }
    
    enum CodingKeys: String, CodingKey {
        case url
    }
}

struct Token: Codable {
    let accessToken: String
    
    enum CodingKeys: String, CodingKey {
        case accessToken = "access_token"
    }
}

这就是我在模型中编写的方式。

class DataObserverSpotify: ObservableObject {
    
    @Published var artistName = ""
    @Published var token = ""
    @Published var fetchResult = [PodcastItemsDetailsSpotify]()
    var subscriptions: Set<AnyCancellable> = []
    let podsURLComponents = PodsFetcher()
    
    init() {
        getToken()
        $artistName
            .debounce(for: .seconds(2), scheduler: RunLoop.main)
            .removeDuplicates()
            .compactMap { query in
                let url = self.podsURLComponents.makeURLComponentsForSpotify(withName: query, tokenAccess: self.token)
                return URL(string: url.string ?? "")
            }
            .flatMap(fetchDatatesting)
            .receive(on: DispatchQueue.main)
            .assign(to: \.fetchResult, on: self)
            .store(in: &subscriptions)
    }
    
     
    func fetchDatatesting(url: URL) -> AnyPublisher<[PodcastItemsDetailsSpotify], Never> {
        URLSession.shared.dataTaskPublisher(for: url)
            .map(\.data)
            .decode(type: DataResponseSpotify.self, decoder: JSONDecoder())
            .map(\.episodes.items)
            .replaceError(with: [])
            .eraseToAnyPublisher()
    }
    
    
    func getToken() {
        let parameters = "grant_type=refresh_token&refresh_token=[example-refresh-token]"
        let postData =  parameters.data(using: .utf8)
        var request = URLRequest(url: URL(string: "https://accounts.spotify.com/api/token")!,timeoutInterval: Double.infinity)
        request.addValue("Basic exampleBasicAuth=", forHTTPHeaderField: "Authorization")
        request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.addValue("inapptestgroup=; __Host-device_id=example_id; __Secure-example=; csrf_token=example", forHTTPHeaderField: "Cookie")

        request.httpMethod = "POST"
        request.httpBody = postData

        URLSession.shared.dataTask(with: request) { data, response, error in
            if let data = data {
                let decoder = JSONDecoder()
                decoder.dateDecodingStrategy = .iso8601
                if let token = try? decoder.decode(Token.self, from: data) {
                    DispatchQueue.main.async {
                        self.token = token.accessToken
                    }
                    return
                }
            }
            print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
        }.resume()
    }
    

}

1 个答案:

答案 0 :(得分:1)

您调用getToken来设置令牌异步,但是假设令牌在.compactMap内部可用,并且没有等待,您立即尝试创建URL-并且尚未到那时为止。

因此,您必须“等待”它,使用Combine,可以用多种方法进行操作。

当前最简单(最便宜)的更改是将$token$artistName发布者合并:

$artistName
    .debounce(for: .seconds(2), scheduler: RunLoop.main)
    .removeDuplicates()
    .combineLatest($token.filter { !$0.isEmpty }) // waits for a non-empty token
    .compactMap { (query, token) in
       var url = ... // construct URL from query and token
       return url
    }
    .flatMap(fetchDatatesting)
    .receive(on: DispatchQueue.main)
    // I used sink, since assign causes a self retain cycle
    .sink { [weak self] result in self?.fetchResult = result }
    .store(in: &subscriptions)
      

但是您确实应该重新设计getToken方法-它编写得不好,因为它是一个异步函数,但是它没有完成处理程序。

例如,像这样:

func getToken(_ completion: @escaping (Token) -> Void) {
   // set-up, etc..   
   URLSession.shared.dataTask(with: request) { data, response, error in
      // error handling, etc...
      if let token = try? decoder.decode(Token.self, from: data) {
          completion(token)
      }

   }.resume()
}
// usage
getToken { 
   self.token = $0
}

或者,由于您使用的是Combine,因此您可以对其进行重新编写以返回发布者(为了确保简洁,我在这里忽略了错误):

func getToken() -> AnyPublisher<Token?, Never> {
   // ...
}