简体   繁体   中英

Alamofire Serialization is Too Slow

For some reason I'm getting really long times for serialization. I'm using Alamofire and ObjectMapper to process request responses and deserialize the data into objects.

This is the request I send:

Alamofire.request(Constants.apiRequests.paloozas, method: .get, parameters: params, headers: HeaderManager.sharedInstance.authHeader!).validate().responseObject { (response: DataResponse<PaginatedPaloozas>)
    // handle response
}

This is the PaginatedPaloozas object:

struct PaginatedPaloozas: Mappable {

    var meta: PageMetaData?
    var paloozas: [Palooza]?

    init?(map: Map) { }

    mutating func mapping(map: Map) {
        self.meta <- map[Constants.PaginatedPaloozas.meta]
        self.paloozas <- map[Constants.PaginatedPaloozas.paloozas]
    }

}

This is my Palooza object:

struct Palooza: Mappable {

var id: Int!
var startingLocation: Place!
var endingLocation: Place!
var startTime: Date!
var endTime: Date?
var note: String?
var perPersonPrice: String!
var seats: Int!
var chat: String?
var completedAt: Date?
var numSeatsAvailable: Int!
var distance: Float?
var bookingFee: String!
var totalPrice: String!
var status: PaloozaStatus!
var code: String?
var involvement: Involvement!
var bookings: [Booking]?
var owner: Owner?

enum PaloozaStatus: String {
    case pending = "pending"
    case inProgress = "in_progress"
    case cancelled = "cancelled"
    case completed = "completed"
}

enum Involvement: String {
    case owner = "owner"
    case presenter = "presenter"
    case none = "none"
}

init?(map: Map) { }

mutating func mapping(map: Map) {
    self.id <- map[Constants.Palooza.id]
    self.startingLocation <- map[Constants.Palooza.startingLocation]
    self.endingLocation <- map[Constants.Palooza.endingLocation]
    self.startTime <- (map[Constants.Palooza.startTime], CustomDateTransform())
    self.endTime <- (map[Constants.Palooza.returnTime], CustomDateTransform())
    self.note <- map[Constants.Palooza.note]
    self.perPersonPrice <- map[Constants.Palooza.perPersonPrice]
    self.seats <- map[Constants.Palooza.seats]
    self.chat <- map[Constants.Palooza.chat]
    self.completedAt <- (map[Constants.Palooza.completedAt], CustomDateTransform())
    self.numSeatsAvailable <- map[Constants.Palooza.numSeatsAvailable]
    self.distance <- map[Constants.Palooza.distance]
    self.bookingFee <- map[Constants.Palooza.bookingFee]
    self.totalPrice <- map[Constants.Palooza.totalPrice]
    self.status <- (map[Constants.Palooza.status], EnumTransform<PaloozaStatus>())
    self.code <- map[Constants.Palooza.code]
    self.involvement <- (map[Constants.Palooza.involvement], EnumTransform<Involvement>())
    self.bookings <- map[Constants.Palooza.bookings]
    self.owner <- map[Constants.Palooza.owner]
}

}

I only have 5 paloozas and it's taking 10 seconds to serialize. I tried using a different queue in the Alamofire Request but it's not making any difference. Please help me speed up this process.

It turns out I was slowing down the serialization process because I was trying to grab a UIImage from a URL during the process. I hope this helps someone else who made a dumb mistake like me haha.

I would already be about the connection and server problem if network speed is 
slow. Try using a different endpoint from a different server to test the 
speed.If its faster then the problem is on your server. Since your 
implementation is correct then definitely it is server problem. alamofire has 
nothing to do on handling the issue if the problem is on the server or 
connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM