2015-09-20 137 views
0

我得拉刷新时重新加载表中存在的表重新载入数据前一秒钟的延迟,除了伟大的工作。拉刷新:刷新数据延迟

难道我只是有一些小东西出来的地方?有任何想法吗?

viewDidLoad

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.refreshControl?.addTarget(self, action: "handleRefresh:", forControlEvents: UIControlEvents.ValueChanged) 
    self.getCloudKit() 
} 

handleRefresh为拉刷新:

func handleRefresh(refreshControl: UIRefreshControl) {  
    self.objects.removeAll() 
    self.getCloudKit() 
    dispatch_async(dispatch_get_main_queue(), {() -> Void in 
     refreshControl.endRefreshing() 
    }) 
} 

需要在两个地方的数据,所以创建了一个功能它getCloudKit

func getCloudKit() { 
    publicData.performQuery(query, inZoneWithID: nil) { results, error in 
     if error == nil { // There is no error 
      for play in results! { 
       let newPlay = Play() 
       newPlay.color = play["Color"] as! String 
       self.objects.append(newPlay) 
      } 

      dispatch_async(dispatch_get_main_queue(), {() -> Void in 
       self.tableView.reloadData() 
      }) 

     } else { 
      print(error) 
     } 
    } 
} 

tableView

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 
    let object = objects[indexPath.row] 
    if let label = cell.textLabel{ 
     label.text = object.matchup 
    } 
    return cell 
} 
+0

在调用模块 – Paulw11

+0

@ Paulw11之前,您不应该调用'endRefreshing'感谢您的回应!我认为现在有道理。不过,我会怎么做,在代码中,我有,因为我不想把'endRefreshing'我'getCloudKit'功能权限(因为我只需要使用'endRefreshing'为拉来刷新不'viewDidLoad'吧? )? – SRMR

+1

无论如何你都可以调用endRefreshing。如果控制器不处于refreshingg状态,它将忽略它 – Paulw11

回答

2

这是你应该怎么做这个:

  1. 在你handleRefresh功能,添加一个布尔跟踪过程中的刷新操作 - 说isLoading
  2. 在你getCloudKit函数之前重新加载表视图调用endRefreshing功能,如果isLoading是真实的。
  3. 重置isLoadingfalse
  4. 重要 - 在刷新操作甚至实例化之前,不要删除模型数据。如果在提取数据时出错,该怎么办?只有在getCloudKit函数中得到响应后才能删除它。
  5. 此外,作为一个侧面说明,如果我想你,我会实现一个基于时间戳的方法,我会通过我的最后一个服务数据的时间戳(在这最后一次更新是从服务器所用的时间)到服务器和服务器端将返回我完整的数据只有在时间戳后发生了变化,否则我会期望他们告诉我没有变化。在这种情况下,我会简单地调用endRefreshing函数,并且不会重新载入表中的数据。相信我 - 这样可以节省很多时间,并提供良好的最终用户体验,因为大多数情况下数据没有变化!