problem with NSNotificationCenter not working with applicationDidTerminate

Discussion in 'iOS Programming' started by stoopkitty, Feb 8, 2011.

  1. stoopkitty, Feb 8, 2011
    Last edited: Feb 8, 2011

    stoopkitty macrumors member

    Joined:
    Jan 26, 2010
    #1
    hi, i have the 4.2 sdk. i am running in the simulator, not on the device. i am trying to make an app that, when the view loads, it loads data from a file, and when the app terminates, it writes the changed information (from a UIDatePicker and UITextiField) to the same file, to be loaded in again when the app starts. i want to have this application will terminate method in my view controller file because i am working with local variables and methods and it is easier.
    the problem is, it doesnt even make the file. i have debugged, and it makes the file path correctly and everything, but when it checks to see if there is a file, it is always false because there isnt one. i have confirmed this by looking at my app's documents folder in finder. i am almost sure that my problem is that is not running the application will terminate method, and i looked up how to do this, even followed tutorials using nsnotificationcenter, but it still does not work, my app's documents folder is empty and it (of course) does not load data on start. i have also looked at this thread but have had no luck.


    again, if you dont feel like reading all that, my problem is that my applicationWillTerminate method is not running because of something i did with NSNotificationCenter in my viewDidLoad method. thanks!!

    .h
    Code:
    #import <UIKit/UIKit.h>
    
    @interface saveAndLoadViewController : UIViewController {
    	UIDatePicker *myDate;
    	UITextField *myText;
    }
    
    @property (nonatomic, retain) IBOutlet UIDatePicker *myDate;
    @property (nonatomic, retain) IBOutlet UITextField *myText;
    
    @end
    
    

    .m

    Code:
    #import "saveAndLoadViewController.h"
    
    @implementation saveAndLoadViewController
    
    @synthesize myDate, myText;
    
    - (NSString *) saveFilePath {
    	
    	NSArray *pathArray = 
    		NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:@"savedata.plist"];
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application {    //from saveAndLoadAppDelegate.m file
    	NSArray *values = [[NSArray alloc] initWithObjects:myText.text,myDate.date,nil];
    	[values writeToFile:[self saveFilePath] atomically:YES];
    	[values release];
    }
    
    - (void)viewDidLoad {
    	NSString *myPath = [self saveFilePath];
    	BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
    	
    	if (fileExists) {
    		NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
    		myText.text = [values objectAtIndex:0];
    		myDate.date = [values objectAtIndex:1];
    		[values release];
    	}
    	
    	UIApplication *myApp = [UIApplication sharedApplication];
    	
    	[[NSNotificationCenter defaultCenter] addObserver:self
    											 selector:@selector(applicationWillTerminate:)
    												 name:UIApplicationWillTerminateNotification
    											   object:myApp];
        [super viewDidLoad];
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    @end
    code
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Put some NSLogs in there: check that each method is actually being called.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    writeToFile returns a BOOL indicating success. You should check that value.

    Is the applicationWillTerminate method called from the app delegate?

    In a multitasking world your app can be backgrounded and then killed so if your app supports multitasking it should write out the values for other times than applicationWillTerminate, which might never be called.
     
  4. stoopkitty thread starter macrumors member

    Joined:
    Jan 26, 2010
    #4
    i found the answer!!

    my problem is that applicationWillTerminate wont get called if your application is already suspended in the background, only if it is taking up memory usage and the system kills it. NEW TO IOS 4!! gah, i should read about these things. btw i found out from this article.

    thanks everyone!!
     

Share This Page