I have this functions:
- (NSMutableDictionary *) getUserDataDictionary
{
[userDataDicionary removeAllObjects];
userDataDicionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[self getUserDataDictionaryPath]];
return userDataDicionary;
}
- (int) getIndexOfLastVehicle
{
MyAppDelegate *app = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *tmpUserData = [app getUserDataDictionary];
int lastHighestIndex = -1;
for(id item in [tmpUserData allKeys]){
NSString *keyInArray = (NSString *)item;
if ([keyInArray rangeOfString:@"VEHICLE-"].location != NSNotFound) {
//f.e. "VEHICLE", "1", "TYPE"...or "VEHICLE", "1", "SPZ"...or "VEHICLE", "2", "TYPE" etc
NSArray * separatedComponents = [keyInArray componentsSeparatedByString:@"-"];
int indexOfVehicle = [(NSString *)[separatedComponents objectAtIndex:1] intValue];
if(indexOfVehicle > lastHighestIndex){
lastHighestIndex = indexOfVehicle;
}
}
}
return lastHighestIndex;
}
The problem is:
after this code:
MyAppDelegate *app = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *tmpUserData = [app getUserDataDictionary];
int lastVehicleIndex = [self getIndexOfLastVehicle];
The tmpUserData is EMPTY.
But when I changed order to this:
int lastVehicleIndex = [self getIndexOfLastVehicle];
MyAppDelegate *app = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *tmpUserData = [app getUserDataDictionary];
The tmpUserData is correctly filled.
Can someone explain this behavior?
Thanks
One of you problems lies in the method
getUserDataDictionary. You are callingremoveAllObjectswhich does not releases the userDataDictionary. You have to release it instead of removingAllObject. The actual release will release all it’s objects for you.