Skip to content

Instantly share code, notes, and snippets.

@codecat15
Created March 15, 2021 16:20
Show Gist options
  • Save codecat15/0ac54e150159f7c8a9c4b2d2b541781c to your computer and use it in GitHub Desktop.
Save codecat15/0ac54e150159f7c8a9c4b2d2b541781c to your computer and use it in GitHub Desktop.
using api with operation queue dependencies
import UIKit
// MARK: - SyncManager
struct SyncManager
{
func GetRecordsInSync()
{
debugPrint("inside the GetRecordsInSync")
let animalOperation = BlockOperation()
animalOperation.addExecutionBlock {
let animals = Animals()
animals.getAnimals { (animalCollection) in
animalCollection?.forEach({ (animal) in
debugPrint("name of animal = \(animal.name)")
})
}
}
let employeeOperation = BlockOperation()
employeeOperation.addExecutionBlock {
let emp = Employee()
emp.getEmployee { (employeeCollection) in
employeeCollection?.forEach({ (employee) in
debugPrint("employee name = \(employee.name)")
})
}
}
employeeOperation.addDependency(animalOperation)
let operationQueue = OperationQueue()
operationQueue.addOperation(animalOperation)
operationQueue.addOperation(employeeOperation)
}
}
// MARK: - Animals struct
struct Animals
{
func getAnimals(handler:@escaping(_ result: [Animal]?)-> Void)
{
debugPrint("inside the getAnimals function")
var urlRequest = URLRequest(url: URL(string: "https://api-dev-scus-demo.azurewebsites.net/api/Animal/GetAnimals")!)
urlRequest.httpMethod = "get"
debugPrint("going to call the http utility for animal request")
HttpUtility.shared.getData(request: urlRequest, response: AnimalResponse.self) { (result) in
if(result != nil)
{
debugPrint("got the animal response from api")
handler(result?.data)
}
}
}
}
// MARK: - Employee struct
struct Employee
{
func getEmployee(handler:@escaping(_ result: [EmployeeData]?)-> Void)
{
debugPrint("inside the get employee function")
var urlRequest = URLRequest(url: URL(string: "https://api-dev-scus-demo.azurewebsites.net/api/Employee/GetEmployee?Department=mobile&UserId=15")!)
urlRequest.httpMethod = "get"
debugPrint("going to call the http utility for employee request")
HttpUtility.shared.getData(request: urlRequest, response: EmployeeResponse.self) { (result) in
if(result != nil)
{
debugPrint("got the emloyee response from api")
handler(result?.data)
}
}
}
}
// MARK: - HttpUtility
struct HttpUtility
{
static let shared = HttpUtility()
private init(){}
func getData<T:Decodable>(request: URLRequest, response: T.Type, handler:@escaping(_ result: T?)-> Void)
{
URLSession.shared.dataTask(with: request) { (data, httpUrlResponse, error) in
if(error == nil && data != nil && data?.count != 0) {
//let str = String(data: data!, encoding: .utf8)
//debugPrint(str)
do {
let result = try JSONDecoder().decode(response, from: data!)
handler(result)
} catch {
debugPrint(error.localizedDescription)
}
}}.resume()
}
}
// MARK: - AnimalResponse
struct AnimalResponse : Decodable
{
let errorMessage: String?
let data: [Animal]?
}
// MARK: - Animal
struct Animal: Decodable {
let name: String
let image: String
}
// MARK: - Welcome
struct EmployeeResponse: Decodable {
let errorMessage: String?
let data: [EmployeeData]?
}
// MARK: - Datum
struct EmployeeData: Decodable {
let name, email, id,joining: String
}
let syncManager = SyncManager()
syncManager.GetRecordsInSync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment