Search code examples
iosswiftalamofire

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


I created an Event App, where in the app is used to register the participants for the event. The list of participants will be pulled to an API url. When pulling 20+ to 400+ participants it takes 1 secs to 3 mins. But when pulling thousands of participants it takes 15 mins or more to be completed. I can't figured it out, if the internet connection is the problem or the device since in my device I have another app installed where in it also pulls thousands of data but it only takes 5 mins to complete. Hope I did explain well. Please help me to solve it because I'am on the testing phase for the user. If you need my code for pulling data I included below.

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)

                
            }
        }
    })
}

Solution

  • Two ideas that will help speed things up.

    1. paginate the API so you don't have to download all of the Attendees in one request. The user can't see them all at once, so why download them all at once?

    2. Have the server sort the attendees before sending them to you, that way you don't have to take the time to do it yourself.