I’m having really weird situation. I create a singletone object of a class named “Profile like this:
static Profile *currentProfile;
+ (Profile *)currentProfile
{
@synchronized(self)
{
if (currentProfile == nil)
currentProfile = [[Profile alloc] init];
}
return currentProfile;
}
- (id)init
{
self = [super init];
if (self)
{
// Initialization code here.
isChecked = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isChecked"] boolValue];
if (isChecked)
{
NSLog(@"isChecked block is called");
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"myEncodedObjectKey"];
self = (Profile *) [NSKeyedUnarchiver unarchiveObjectWithData:data];
[self retain];
for (int i = 0; i < self.avatar.count; i++)
[self.avatar replaceObjectAtIndex:i withObject:[UIImage imageWithData:[self.avatar objectAtIndex:i]]];
}
else
{
password = @"";
pfefferID = @"";
email = @"";
avatar = [[NSMutableArray alloc] initWithObjects:
[UIImage imageNamed:@"empty_image.png"],
[UIImage imageNamed:@"empty_image.png"],
[UIImage imageNamed:@"empty_image.png"],
[UIImage imageNamed:@"empty_image.png"],
[UIImage imageNamed:@"empty_image.png"],nil];
isBoy = YES;
friends = [[NSMutableDictionary alloc] init];
rating = 0;
}
}
return self;
}
In init method i check a certain condition stored in NSUserDefaults by using BOOL variable named “isChecked”. isChecked is equal to YES and everything works fine. But… i create this Profile object in AppDelegate file like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
users = [[NSMutableDictionary alloc] init];
usersForLeaderboardFromServer = [[NSMutableDictionary alloc] init];
listOfFriendsFromServer = [[NSMutableDictionary alloc] init];
currentProfile = [Profile currentProfile];
sessionID = 0;
if (!currentProfile.isChecked)//why????
NSLog(@"not checked");
if (currentProfile.isChecked)
{
[self getUsersForLeaderboardFromServer];
MainMenuView *mainMenu = [[[MainMenuView alloc] init] autorelease];
[self.navigationController pushViewController:mainMenu animated:YES];
}
}
So the same isChecked variable which a moment (far less than a moment actually) ago was equal to YES gets equal to NO when being used in application didFinishLaunchingWithOptions method by accessing it via dot. What’s going on? I’m able to handle it but i’m just curious about this situation. Do you know what’s wrong with it?
You’re reassigning
selfin init, so you’re returning the new object rather than the one you setisCheckedon. See this code:It’s slightly awkward to do things like you’ve got – I would certainly not recommend replacing it in the way you have. For a start, the value you set to the static
currentProfileis not being updated when you reassignselfso that’s still the old one. And also you’re not releasing the oldselfwhen you reassign.To fix it you could do something like this:
But I don’t really advocate that personally. I suggest you load in the object from your archive and then proceed to update yourself with it rather than reassigning self.