Hi Brian this a question about good practices, I create for a SearchController a file where I put the ServiceNetwork class I Called SearchNetwork, and that service network have a parent class Service... and there I put the singleton, and the function to fetch results in the search text,... do you thing this is a good practice? for every class? create a file to get the correspond functions, because maybe in Service class, could be very big
and the Service class have only the fetchGenericJsonData function... something like this....
and what do you thing about this?
class Network {
// Generic json function
func fetchGenericJsonData<T: Decodable>(urlString: String, completion: @escaping (T?, Error?) -> Void) {
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error {
completion(nil, error)
}
guard let data = data else { return }
do {
let appGroup = try JSONDecoder().decode(T.self, from: data)
completion(appGroup, nil)
} catch let error as NSError {
completion(nil, error)
}
}.resume()
}
}
class SearchNetwork: Network {
static var shared = SearchNetwork()
func fetchApps(searchTerm: String, completion: @escaping (SearchResult?, Error?) -> Void) {
let urlString = "https://itunes.apple.com/search?term=\(searchTerm)&entity=software"
fetchGenericJsonData(urlString: urlString, completion: completion)
}
}
by the way I actually use Xcode 11 and everything goes fine