如何在 swift 中使用 Alamofire 从 API 中提取数千条数据?

How to pull thousands of data from API using Alamofire in swift?

我创建了一个活动应用程序,在该应用程序中用于注册活动参与者。参与者名单将被拉到 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({ [=12=].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)


            }
        }
    })
}

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

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

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