2015-08-28 87 views
0


所以我有一张表,它从我的数据库中获取一些数据并将其显示在他的单元格中。我做了一个刷新按钮,重新载入我的代码,这样用户就可以看到有人添加到数据库中的新条目,当您点击它时。
这是我的刷新按钮的代码看起来像:
以每30秒快速刷新一次tableview的数据

@IBAction func refreshButton(sender: AnyObject) { 

     textArray.removeAllObjects() 

     let predicate = NSPredicate(value: true) 
     let query = CKQuery(recordType: "Conversation", predicate: predicate) 

     query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] 

     publicCloudDatabase?.performQuery(query, inZoneWithID: nil, 
      completionHandler: ({results, error in 

       if (error != nil) { 
        dispatch_async(dispatch_get_main_queue()) { 
         self.notifyUser("Cloud Access Error", 
          message: error.localizedDescription) 
        } 
       } else { 
        if results.count > 0 { 
         var record = results[0] as! CKRecord 
         self.currentRecord = record 

         dispatch_async(dispatch_get_main_queue()) { 
          for x in results{ 
           self.textArray.addObject(x.objectForKey("message") as! String) 
           self.tableView.reloadData() 
          } 

         } 
        } else { 
         dispatch_async(dispatch_get_main_queue()) { 
          self.notifyUser("No Match Found", 
           message: "No record matching the address was found") 
         } 
        } 
       } 
      })) 
    } 

我想删除按钮,使表的刷新每30秒(在应用程序的背景 - 我不希望用户意识到这一点),这是可能的吗?如果是,那该怎么办?
谢谢!

回答

0

使用定时器是您可以实现的一种方式, In ViewDidload

self.myTimer = NSTimer(timeInterval: 30.0, target: self, selector: "refresh", userInfo: nil, repeats: true) 
    NSRunLoop.mainRunLoop().addTimer(self.myTimer, forMode: NSDefaultRunLoopMode) 

func refresh() { 
    // a refresh of the table 
} 
0

1.每30秒进行一次查询。

2.比较旧数据和新数据的不同。

3.插入不同部分通过使用该API

  • (无效)insertRowsAtIndexPaths:(NSArray的*)indexPaths withRowAnimation:(UITableViewRowAnimation)动画

而非reloadData