如何使用大JSON数据更快地填充tableview

时间:2015-04-23 15:56:19

标签: ios json swift

我有一个应用程序,使用Alamofire从我的服务器检索json数据。我使用SwiftyJSON解析这个json数据,然后填充我的表视图。但是我的性能有问题,它非常慢,用接收的数据加载表视图需要6秒(90个订单),并且随着订单数量的增加需要更长的时间。我认为问题不在于Alomofire。我如何更快地解析和填充表格视图,类似于推特或脸书?这就是我所拥有的

// Order class
class Order {
  var id:Int
  var firstName:String
  var lastName:String
  var shippingAddress1:String
  var dateTime:String
  var total:String
  var status:String

  init(id:Int, firstName:String, lastName:String, shippingAddress1:String, dateTime:String, total:String, status:String){
    self.id = id
    self.firstName = firstName
    self.lastName = lastName
    self.shippingAddress1 = shippingAddress1
    self.dateTime = dateTime
    self.total = total
    self.status = status
 }
}

下面是我的订单表视图控制器代码。

 var api = API() // Making Alamofire requests
 var orders = [Order]() // Order model

 override func viewDidLoad() {
    super.viewDidLoad()
    self.makeCall() // Make api call to server
}

func makeCall(){
    // Get orders
    self.api.call(to: "orders?filter[limit]=100") { (result:NSDictionary?, error) -> () in
        if error == nil{
            self.setOrdersData(result!) // Received data
        }
    }
}

func setOrdersData(data:NSDictionary){
    let json = JSON(data) // SwiftyJSON
    let qos = Int(QOS_CLASS_USER_INITIATED.value)
    // Process it in the background
    dispatch_async(dispatch_get_global_queue(qos, 0), { () -> Void in

        // Loop through the orders and parse them
        for index in 0...json["orders"].count-1{
            let id:Int = json["orders"][index]["id"].intValue
            let firstName = json["orders"][index]["shipping_address"]["first_name"].stringValue
            let lastName = json["orders"][index]["shipping_address"]["last_name"].stringValue
            let shippingAddress1 = json["orders"][index]["shipping_address"]["address_1"].stringValue
            let dateTime = json["orders"][index]["completed_at"].stringValue
            let total = json["orders"][index]["total"].stringValue
            let status = json["orders"][index]["status"].stringValue

            // Append order to Order model
            self.orders.append(Order(id: id, firstName: firstName, lastName: lastName, shippingAddress1: shippingAddress1, dateTime: dateTime, total: total, status: status))
        }
        // Get the main thread
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.orders.sort({$0.id > $1.id}) // Sort orders
            self.tableView.reloadData() // Reload tableView
        })
    })
}

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:OrdersTableViewCell = tableView.dequeueReusableCellWithIdentifier("orderCell", forIndexPath: indexPath) as! OrdersTableViewCell
    let order = self.orders[indexPath.row]

    cell.labelAddress.text = order.shippingAddress1.capitalizedString
    var fullName:String = order.firstName + " " + order.lastName
    cell.labelFullName.text = fullName.capitalizedString

    cell.labelAmount.text = "$\(order.total)"


    cell.labelStatus.text = order.status
    cell.labelTime.text = "\(order.dateTime)"
    return cell
}

1 个答案:

答案 0 :(得分:1)

解决性能问题的第一步是分析。哪部分代码很慢?要么使用Xcode中的性能分析工具,要么将这些代码拆分成单独的函数(无论如何都是个好主意)并测量每个函数的运行时间。当您可以看到代码的哪一部分花费最多时间时,您可以制定计划来更改该部分。