Swift:用于网络请求的单独类

时间:2022-10-13 13:04:46

friends!, I am new to Swift. I need to make multiple API requests in one of my view controllers. If I put all the code in ViewController it will be messy. So I am trying to develop this simple architecture to separate the concepts but I'm not sure whether it's the best approach to handle this situation.

朋友们!我是斯威夫特新来的。我需要在一个视图控制器中创建多个API请求。如果我把所有的代码都放到ViewController中会很混乱。所以我正在开发这个简单的体系结构来分离概念,但是我不确定这是否是处理这种情况的最佳方法。

/*-----------------------------------------------------*/
/* RestClient.swift */
/*-----------------------------------------------------*/
protocol RestClientDelegate {
    func dataDidLoad(json:String)
}
class RestClient {

    var delegate:RestClientDelegate

    //Handles all the network codes and exceptions
    func makeGetRequest(url){
        //send a get request to the server  
        //on error - handle erros 
        //on success - pass Json response to HttpUser class
        delegate.dataDidLoad(jsonOutput)
    }

}

/*-----------------------------------------------------*/
/* HttpUser.swift */
/*-----------------------------------------------------*/
protocol UserDelegate(){
    func usersDidLoad(usersArray:[User])//User object array
}
class HttpUser, RestClientDelegate {

    var delegate:UserDelegate

    func getUsers(){
        //Use rest client to make an api call
        var client = RestClient()
        client.delegate = self
        client.makeGetRequest("www.example.com/users")
    }

    //Once RestClient get the json string output, it will pass to
    //this function
    func dataDidLoad(json:String){
        //parse json - Handles json exceptions
        //populate user objects in an array
        //pass user array to the ViewController
        delegate.usersDidLoad(usersArray:[User])
    }

}

/*-----------------------------------------------------*/
/* UserViewController.swift */
/*-----------------------------------------------------*/
class UserViewController:UIViewController, UserDelegate {

    override viewDidLoad(){
        super.viewDidLoad()

        //Ask http user class to retrieve users
        var httpUser = HttpUser()
        httpUser.delegate = self
        httpUser.getUsers()
    }

    //Callback function to get users array
    func usersDidLoad(usersArray:[User]) {
        //now you have users object array
        //populate a table view
    }

}
  1. RestClient.swift - Make the API request and pass a json output. This class contains all the code related to network GET/POST requests. I can modify this class in the future without affecting other classes.
  2. RestClient。swift -发出API请求并传递json输出。这个类包含与网络GET/POST请求相关的所有代码。我可以在将来修改这个类而不影响其他类。
  3. HttpUser.swift - Get json output make an Users Array and pass it. This class does not care about network requests. It will just handle the JSON response and parse it into an object array. I will be having multiple of these. (ex: HttpBlogs, HttpComments)
  4. HttpUser。获取json输出,生成用户数组并传递它。这个类不关心网络请求。它将处理JSON响应并将其解析为对象数组。我将会有这些的倍数。(例:HttpBlogs HttpComments)
  5. UserViewController.swift - Get Users Array and populate a table view. This will handle only sections related to the UI.
  6. UserViewController。获取用户数组并填充表视图。这将只处理与UI相关的部分。

Can you tell me is this approach is good to follow? Is there a much better way to achieve this?

你能告诉我这个方法好吗?有更好的方法来实现这一点吗?

Thanks a lot everyone!

非常感谢大家!

-Please note: I don't want to use 3rd party libraries like Alamofire in this case.

-请注意:在这种情况下,我不想使用像Alamofire这样的第三方库。

1 个解决方案

#1


1  

In general, this is a good approach. You may want to think about displaying errors (ex: no internet connection) on the user interface for better user experience. In your example, you are handling your errors in RestClient class, but in case of any error, it is not upstreamed to UserViewController class to handle on the user interface.

总的来说,这是一个很好的方法。您可以考虑在用户界面上显示错误(例如:没有internet连接),以获得更好的用户体验。在您的示例中,您正在处理RestClient类中的错误,但是如果出现任何错误,它不会被上传到UserViewController类以在用户界面上处理。

#1


1  

In general, this is a good approach. You may want to think about displaying errors (ex: no internet connection) on the user interface for better user experience. In your example, you are handling your errors in RestClient class, but in case of any error, it is not upstreamed to UserViewController class to handle on the user interface.

总的来说,这是一个很好的方法。您可以考虑在用户界面上显示错误(例如:没有internet连接),以获得更好的用户体验。在您的示例中,您正在处理RestClient类中的错误,但是如果出现任何错误,它不会被上传到UserViewController类以在用户界面上处理。