【问题标题】:How to pull thousands of data from API using Alamofire in swift?如何在 swift 中使用 Alamofire 从 API 中提取数千条数据?
【发布时间】:2019-04-19 04:17:39
【问题描述】:

我创建了一个活动应用程序,该应用程序用于注册活动的参与者。参与者列表将被拉到 API url。当拉动 20+ 到 400+ 参与者时,需要 1 秒到 3 分钟。但是,当拉动数千名参与者时,需要 15 分钟或更长时间才能完成。我无法弄清楚,如果互联网连接是问题还是设备,因为在我的设备中我安装了另一个应用程序,其中还提取了数千个数据,但只需要 5 分钟即可完成。希望我解释得很好。请帮我解决它,因为我正处于用户的测试阶段。如果您需要我的代码来提取下面包含的数据。谢谢。

APIService.swift

  func getParticipants(enteredPincode: String,
                     participantType: ParticipantType,
                     completionHandler: @escaping (([Attendee]?, NetworkError?) -> Void))

{

    guard let attendeesURL = URL(string: "\(GET_PARTICIPANTS_URL)/\(enteredPincode)/\(participantType)") else {
        completionHandler(nil, .invalidURL)
        return
    }

    let sessionManager = Alamofire.SessionManager.default
    sessionManager.session.getAllTasks { (tasks) in
        tasks.forEach({ $0.cancel() })
    }

    Alamofire.request(attendeesURL, method: .get, encoding: JSONEncoding.default).responseJSON(completionHandler: { (response) in

        guard HelperMethod.reachability(responseResult: response.result) else {
            completionHandler(nil, .noNetwork)
            return
        }



        if let statusCode = response.response?.statusCode {

            switch(statusCode) {
            case 200:
            if let jsonArray = response.result.value as? [[String : Any]] {

                for anItem in jsonArray {
                    if let eventparticipants = anItem["event_participants"] as? [[String : Any]] {
                        var extractedAttendees = [Attendee]()

                        for participants in eventparticipants{
                            let attendee = Attendee.init(JSON: participants)
                            extractedAttendees.append(attendee!)
                            extractedAttendees = extractedAttendees.sorted(by: { (Obj1, Obj2) -> Bool in
                                let Obj1_Name = Obj1.lastName
                                let Obj2_Name = Obj2.lastName
                                return (Obj1_Name.localizedCompare(Obj2_Name) == .orderedAscending)
                            })
                        }
                        completionHandler(extractedAttendees, nil)
                    }
                }
            }


       case 400:
        completionHandler(nil, .badRequest)
       case 404:
        completionHandler(nil, .invalidCredentials)
       case 409:
        completionHandler(nil, .notSuccessful)
       case 500:
        completionHandler(nil, .serverError)
       default:
        completionHandler(nil, .uncapturedStatusCode)


            }
        }
    })
}

【问题讨论】:

  • 连同下面答案中的建议(分页)您可能想查看此会话 - developer.apple.com/videos/play/wwdc2017/709 我不确定 AlamoFire 这些天在幕后做了什么,但您可能想切换到普通URLSession;通过确保您重复使用现有的 HTTPS 会话,您可以显着减少数据传输时间

标签: ios swift api ipad alamofire


【解决方案1】:

两个有助于加快速度的想法。

  1. 对 API 进行分页,这样您就不必在一个请求中下载所有与会者。用户不能一次看到它们,为什么要一次下载它们呢?

  2. 让服务器在将与会者发送给您之前对其进行排序,这样您就不必花时间自己做。

【讨论】:

  • 谢谢。我只是省略了代码中的排序内容,让服务器为我排序。现在可以提取数据了。
猜你喜欢
  • 2019-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多