2015-08-28 116 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