0
votes

This is simple version my Giphy.com client. I'm using Moya and Moya-ObjectMapper with Rxswift to make network requests.

Here's my code:

import UIKit
import RxSwift
import RxCocoa
import Moya
import Mapper
import Moya_ModelMapper

struct Gif: Mappable {

    let title: String

    init(map: Mapper) throws {
        try title = map.from("source_post_url")
    }
}

class ViewController: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!

    let provider = MoyaProvider<GiphyEndpoint>()
    let disposeBag = DisposeBag()

    var text: Observable<String> {
        return searchBar.rx.text
            .orEmpty
            .debounce(0.5, scheduler: MainScheduler.instance)
            .distinctUntilChanged()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let data = getDataWithString()

        data.bind(to: tableView.rx.items(cellIdentifier: "Cell")) { row, model, cell in
            cell.textLabel?.text = model.title
            }
            .disposed(by: disposeBag)

    }

    func getDataWithString() -> Observable<[Gif]> {
        return text
            .observeOn(MainScheduler.instance)
            .flatMapLatest({ text -> Observable<[Gif]> in
                return self.request(text)
            })
    }

    func request(_ string: String) -> Observable<[Gif]> {
        return provider.rx.request(.search(string))
            .map(to: [Gif].self)
            .asObservable()
    }
}

return an error message:

Thread 1: Fatal error: Binding error: jsonMapping(Status Code: 200, Data Length: 137)

This is the endpoint code:

import Foundation
import Moya

private extension String {

    var URLEscapedSearchString: String {
        return self.lowercased().replacingOccurrences(of: " ", with: "+")
    }
}

enum GiphyEndpoint {
    case trending
    case search(_ string: String)
}

extension GiphyEndpoint: TargetType {

    var baseURL: URL {
        return URL(string: "https://api.giphy.com")!
    }

    var path: String {
        switch self {
        case .trending: return "/v1/gifs/trending"
        case .search: return "/v1/gifs/search"
        }
    }

    var method: Moya.Method {
        return .get
    }

    var sampleData: Data {
        return Data()
    }

    var task: Task {
        switch self {
        case .search(let searchText):
            return .requestParameters(parameters: ["q": searchText],
                                        encoding: URLEncoding.default)
        case .trending: return .requestPlain
        }
    }

    var headers: [String : String]? {
        return ["api_key": "YUAoFkF42JjpIRdLWmJu70IGDXilhltD"]
    }
}

Where is my code problem? Thank you!

1

1 Answers

0
votes

I haven't worked with Moya before, but have investigated it for the purposes of answering this question.

The response you get has the following format:

{
    "meta": {...}, 
    "data": [.., .., ..], 
    "pagination" : {...}
}

The error you get indicates the JSON mapping was not working properly. Due to some internals, this error is converted to a fatal error.

The Gif objects are not at the root of the JSON, but inside "data". The solution: .map(to: [Gif].self, keyPath: "data").