Kenny
Kenny

Reputation: 39

Refresh TableView except pull to refresh

I use Parse to save data and every time I add new data to server,TableView doesn't refresh and get new data

Is any way to refresh TableView except pull refresh ?

Upvotes: 1

Views: 574

Answers (4)

HardikDabhi
HardikDabhi

Reputation: 2952

First of all Add Observer method which will notify when you.

NotificationCenter.default.post(name: Notification.Name("ReloadTableData"), object: nil)

Register that observer method in your view controller on which tableview you used.

 NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("ReloadTableData"), object: nil)

and create one function and reload your tableview

This might by your possible solutions for reloading without pull to refresh.

Upvotes: 2

ChunTingLin
ChunTingLin

Reputation: 970

Implement a push notification or use LiveQuery on parse-server.

LiveQuery allows you to subscribe to a Parse.Query you are interested in. Once subscribed, the server will notify clients whenever a Parse.Object that matches the Parse.Query is created or updated, in real-time.

LiveQuery is already support IOS.

for more detail see the hyperLink.

Upvotes: 0

Kirit Modi
Kirit Modi

Reputation: 23407

first of all Adding refresh control to the tableview. So add the below code in ViewDidLoad.

    let refresh = UIRefreshControl()
    refresh.tintColor = UIColor.redColor()
    refresh.attributedTitle = NSAttributedString(string:"Loading..!!", attributes: [NSForegroundColorAttributeName: UIColor.redColor()])
    refresh.addTarget(self, action: "handleRefresh:", forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refresh)
    self.tableView.sendSubviewToBack(refresh)

Now Calling the method when you Pull-To-refresh Tableview.

func handleRefresh(refreshControl : UIRefreshControl){
    self.tableView.reloadData()
    refreshControl.endRefreshing()
}

Refresh Control :

enter image description here

Upvotes: 0

Alex Zanfir
Alex Zanfir

Reputation: 573

You can use this:

self.tableView.reloadData()

Whenever the user get back to the app or opens the app. You can add in in the method

applicationWillEnterForeground

If the user is still in the app while the updates are being performed, then use push notification. In the method

appDidReceiveRemoteNotification 

reload the data. You have to register for remote notification and all the process before you receive any push notification, but that is another issue.

Upvotes: 0

Related Questions