Stk Stocki
Stk Stocki

Reputation: 139

saving app data fields upon user termination or app going to background

My App need to save some data (around 100 double fields that is stored in array and some few other string data as well). I have a method -(void) case2save already in place to save data to the NSDocumentDirectory when the user initiates it and all is working well, however I need to save the same data when the App is terminated either by the user (home) or by the iOS.

I'm trying to use the applicationDidEnterBackground from the delegate to call that same method i use in the view controller .m class but since the function is per the object (not class method) i can't, but then if i'm making it to be class method i don't have the actual data which is part of the object... so that's the issue.

An idea how to resolve that issue and save the data when App is terminated or goes to the background ?

Thanks.

Upvotes: 0

Views: 272

Answers (2)

Abhishek
Abhishek

Reputation: 2253

you can make that function in appdelegate , or just declare a data member of that class in appdelegate.h and make it property and set this data member as self in that function where you are trying to save data , use following.....

//in Your Appdelegate.h suppose you have viewController named AbcViewController ....
AbcViewController  *abcVC;

create property ...
@property (nonatomic,retain) AbcViewController  *abcVC;

//in Your Appdelegate.m
@synthesize  abcVC;

//Now in function where you are trying to save database .....

AppdelegateClass   *app = (AppdelegateClass*)[[UIApplication sharedApplication]delegate];
 app.abcVC = self;

 //now call that function from applicationDidEnterBackground with the help of this object...

Hope this will resolve your issue...

Upvotes: 1

Phillip Mills
Phillip Mills

Reputation: 31016

There is a notification sent, called UIApplicationDidEnterBackgroundNotification. You can listen for that in your view controller and do whatever you need in response.

Upvotes: 0

Related Questions