user8169082
user8169082

Reputation:

How to refresh TableView after app is about to become active?

I'd like for my TableView to reloadData once my app is about to enter the foreground. I've done the following but it doesn't work:

override func viewWillAppear(_ animated: Bool) {
    //registering ContactsVC as an Observer
    NotificationCenter.default.addObserver(self,selector: #selector(willEnterForeground),name: NSNotification.Name.UIApplicationWillEnterForeground,object: nil)
}


override func viewDidDisappear(_ animated: Bool) {
    //remove ContactsVC as the observer when your view is going away
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}

@objc func willEnterForeground() {

        print("print if willEnterForeground() is called")
        self.tableView.reloadData()

}

The print statement in my function handler does print out on the console but the tableview wont reload.

Upvotes: 3

Views: 2385

Answers (5)

Pranay
Pranay

Reputation: 486

In ViewDidLoad ,

override func viewDidLoad() {
     super.viewDidLoad()
     NotificationCenter.default.addObserver(self, selector:  #selector(self.reloadData(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)

}

In Deinit, remove the observer,

deinit() {
     NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: UIApplication.willEnterForegroundNotification), object: nil)
}

Reload the table in main dispatch queue,

@objc func reloadData(_ notification: Notification?) {
     DispatchQueue.main.async {
      [weak self] in 
      self?.tableview.reloadData()
  }
}

Upvotes: 2

MUHAMMED IQBAL PA
MUHAMMED IQBAL PA

Reputation: 3604

Swift 4.1

in AppDelegate

func applicationWillEnterForeground(_ application: UIApplication) {
   NotificationCenter.default.post(name: NSNotification.Name("ReloadNotification"), object: nil)
}

Then go to your desired View Controller and in ViewDidload

NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData(_:)), name: NSNotification.Name("ReloadNotification"), object: nil)

finally, add the Notification Function in the ViewController

@objc func reloadData(_ notification: Notification?) {
    self.tableview.reloadData()
}

Upvotes: 2

SwiftNinja95
SwiftNinja95

Reputation: 157

try to use dispatchQueue for reloading tableview data.

Refer this link: https://www.raywenderlich.com/60749/grand-central-dispatch-in-depth-part-1

Upvotes: 0

Vicky_Vignesh
Vicky_Vignesh

Reputation: 592

Go to AppDelegate and Add a Notification

[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationToReload" object:nil];

in

- (void)applicationDidBecomeActive:(UIApplication *)application

Then go to your desired View Controller and in ViewDidload add the same notification as

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(forceUpdateSkip:) name:@"NotificationToReload" object:nil ];

Then add the Notification Function in the same ViewController

-(void)forceUpdateSkip:(NSNotification*)notification{

  [tableview reloadData];

}

Instead of tableview replace your table Name.

Now whenever the app get enter Foreground, notification get called, table will be reloaded.

OR

Follow this option,

simply add

[tablename reloadData];

in

-(void)viewWillAppear:(BOOL)animated

Upvotes: 0

Madhur
Madhur

Reputation: 1106

to reload the tableView, you can simply add

tableView.reloadData() 

in your viewWillAppear() method.

Upvotes: -1

Related Questions