-3

Actually the requirement is to reload the data when app comes from background. but it doesn't reload when I come from background. I write the method for reloading data on viewDidLoad.

So, where should I write the code to solve my problem?

Thanks...

Rohan
  • 2,939
  • 5
  • 36
  • 65

4 Answers4

2

As KartikArora is implies above, your viewDidLoad is not called when the app comes from the background to the foreground. So the data is not reloaded.

You could reload the data whenever the view appears instead of when the view is loaded. But then it would reload the data every time the view appears, which you might not want.

You could also have a reload method in your view controller that is called when the app enters foreground triggered via a posted notification.

ader
  • 5,403
  • 1
  • 21
  • 26
1
-(void) viewDidLoad {

[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name: UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)myMethod:(id)not {
// code for save data
}

try this

Igor Bidiniuc
  • 1,540
  • 1
  • 16
  • 27
  • @lgor - thanks for answer. I have tried your code. But the problem is that it starts my app from the starting when i run the app from background but not as background app.. :( – Rohan Mar 02 '12 at 13:43
  • https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html see here more notifications i think you need UIApplicationDidBecomeActiveNotification – Igor Bidiniuc Mar 02 '12 at 15:06
  • 1
    or UIApplicationWillEnterForegroundNotification – Igor Bidiniuc Mar 02 '12 at 15:07
1

Try it in App Delegate File it will work

- (void) applicationWillEnterForeground: (UIApplication *) application
{
 write your code here
}
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
Ankit Gupta
  • 958
  • 1
  • 6
  • 19
  • thanks for reply. I want to know how can i call my ViewController's method from the AppDelegate.? Because the data retrieve code is in viewController. – Rohan Mar 02 '12 at 13:45
  • Then u have to try View Will Appear Method Of View Controller. – Ankit Gupta Mar 02 '12 at 13:54
  • thanks for reply. But the viewWillAppear method doesn't call when app come from the background. – Rohan Mar 03 '12 at 04:33
0

Either you do it in the UIApplicationDelegates

- (void)applicationDidBecomeActive:(UIApplication *)application

method, or you subscribe your object to the UIApplicationDidBecomeActiveNotification.

  • Aligner - thanks for reply. But i want to know how i use my viewController's retriveData method in the AppDelegate's - (void)applicationDidBecomeActive:(UIApplication *)application method , because when i try ,it does not show viewController's method ? – Rohan Mar 02 '12 at 11:25