1

I am trying to store a couple of arrays using NSUserDefaults. I am trying to do everything inside the appDelegate when the application either is terminated or launches. However, I am having a hard time getting it to work.... I am able to access my arrays (projects, tasks, and teamMembers) from any other class in the app including being able to add and remove data to them. SO I know that that they are working, but I can't save data and get it again. Though I have some objective c experience I have never tried to save any app data. Anyone have any suggestions? Here is the code from my AppDelegate

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize tasks;
@synthesize teamMembers;
@synthesize projects;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:     (NSDictionary *)launchOptions
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
tasks = [[NSMutableArray alloc]init];
tasks = [prefs objectForKey:@"tasks"];
NSLog(@"There are this many tasks in TASKS after the app loads %@", [tasks count]);
teamMembers = [prefs objectForKey:@"teamMembers"];
projects = [prefs objectForKey:@"projects"];

// Override point for customization after application launch.
return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application
{
/*
 Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
 Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
 */

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSString *theTasks = [[NSString alloc]initWithString:@"tasks"];
NSString *theTeamMembers = [[NSString alloc]initWithString:@"teamMembers"];
NSString *theProjects = [[NSString alloc]initWithString:@"projects"];

[prefs setObject:tasks forKey:theTasks];
[prefs setValue:teamMembers forKey:theTeamMembers];
[prefs setValue:projects forKey:theProjects];
[prefs synchronize];


}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
 Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
 If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
 */

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


NSString *theTasks = [[NSString alloc]initWithString:@"tasks"];
NSString *theTeamMembers = [[NSString alloc]initWithString:@"teamMembers"];
NSString *theProjects = [[NSString alloc]initWithString:@"projects"];

[prefs setObject:tasks forKey:theTasks];
[prefs setValue:teamMembers forKey:theTeamMembers];
[prefs setValue:projects forKey:theProjects];
[prefs synchronize];

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
 Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
 */

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
tasks = [prefs objectForKey:@"tasks"];
teamMembers = [prefs objectForKey:@"teamMembers"];
projects = [prefs objectForKey:@"projects"];

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
 Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
 */

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
tasks = [prefs objectForKey:@"tasks"];
teamMembers = [prefs objectForKey:@"teamMembers"];
projects = [prefs objectForKey:@"projects"];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
/*
 Called when the application is about to terminate.
 Save data if appropriate.
 See also applicationDidEnterBackground:.
 */

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


NSString *theTasks = [[NSString alloc]initWithString:@"tasks"];
NSString *theTeamMembers = [[NSString alloc]initWithString:@"teamMembers"];
NSString *theProjects = [[NSString alloc]initWithString:@"projects"];

[prefs setObject:tasks forKey:theTasks];
[prefs setValue:teamMembers forKey:theTeamMembers];
[prefs setValue:projects forKey:theProjects];
[prefs synchronize];

}

@end
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rob
  • 2,319
  • 4
  • 20
  • 18
  • what types of objects are stored in arrays ? –  Jan 30 '12 at 22:36
  • @Vince They are custom class objects I have designed. I have one for Task, one for TeamMember, and one for Project. So I guess to answer your question they are NSObjects.. – Rob Jan 30 '12 at 22:37
  • The answer is they are *custom objects*. If your interfaces conform to `NSCoding` protocol, you could (will need) wrap them into `NSData`. See [here](http://stackoverflow.com/q/2315948/971401). –  Jan 30 '12 at 22:40
  • @Vince would I need to encode the actual objects and put them in the Array that way, or would I just encode the Arrays before saving? – Rob Jan 30 '12 at 22:45
  • 1
    Conforming to the protocol, then putting the object in a array, and storing the array should be fine. –  Jan 30 '12 at 22:48
  • This isn't the problem, but you have a bunch of memory leaks. Everywhere you alloc/init strings. Since the strings are static, just put them in the forKey: part of the method call: `[prefs setObject:tasks forKey:@"tasks"];` – Jim Jan 30 '12 at 22:49
  • @Jim Ya thats a good idea. Though, doesn't the new ARC environment take care of releasing everything anyways? I couldn't release the strings even if I wanted to in the current project setup... – Rob Jan 30 '12 at 22:55
  • 2
    Oops. I don't live in an ARC world yet. Maybe this weekend, though. :) – Jim Jan 30 '12 at 22:56
  • I didn't find the instance method {setValue:forKey:} under NSUserDefaults, – Cullen SUN Jan 31 '12 at 05:39

0 Answers0