Reputation: 531
I am encountering a confusing problem in my Objective-C program. I am trying to save data from an NSMutableArray when my program enters the background. I have a static variable called savedResults in my AppDelegate. A view controller manipulates this variable and adds data to it during the lifetime of my program. I have an logical condition to check if savedResults is null, and if it isn't then I need to save the data. Here is my code:
NSString *const kFileName = @"PCFData.bin";
//these are all my static variables..I have to initialize them to something so
//they can be used in other parts of my program with the keyword extern.
NSString *finalTermValue = @"";
NSString *finalClassValue = @"";
NSString *finalTermDescription = @"";
NSMutableArray *savedResults = nil;
@implementation PCFAppDelegate
@synthesize finalTermValue, finalClassValue, finalTermDescription, savedResults;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *fullPath = [docDir stringByAppendingFormat:@"/%@", kFileName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];
if (fileExists) {
savedResults = [NSKeyedUnarchiver unarchiveObjectWithFile:fullPath];
}
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.
}
- (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.
if (savedResults) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *fullPath = [docDir stringByAppendingFormat:@"/%@", kFileName];
[NSKeyedArchiver archiveRootObject:savedResults toFile:fullPath];
}
}
I put a breakpoint in the applicationDidEnterBackgroundMethod to see what is going on. My program never enters the block of code inside the if statement, even though the savedResults array is NOT null. I have also tried testing if ([savedResults count] > 0) and it does not enter the block even though it is greater than zero. Here is a picture of the variables XCode is showing. As you can see, there ARE objects in the array.
I have a feeling XCode is looking at the array declaration above where I set it to nil instead of the actual variable. How do I distinguish these two? Any help would be greatly appreciated. Thanks!
Upvotes: 0
Views: 1256
Reputation: 385870
You have two variables named savedResults
. One is a global variable. The other is an instance variable in the PCFAppDelegate
class, generated by the @synthesize savedResults
statement. The debugger showing you both variables. The instance variable is under the expansion of self
and the global variable is shown to the right of a disclosure triangle and an “S” in a red box.
All of the mentions of savedResults
in methods of PCFAppDelegate
use the instance variable, but mentions in other classes will use the global variable. So some code outside of PCFAppDelegate
is setting the global variable to non-nil, but in
-[PCFAppDelegate applicationDidEnterBackground]`, you can only access the instance variable, which is still set to nil.
Upvotes: 2