From what I've read from other posts, this looks like the correct way to go about this. However, each time I call the code to load the plist it is an empty array (after I call the code to save it).
If anyone knows where I am going wrong your help is appreciated!
Looks like you're dealing with 2 different locations.
Thanks Brian. That seems to be the one of the issues. I'm not sure if I should start another thread or not, but it looks like my NSMutableArray is being autoreleased somewhere along the line too.
When I add a NSMutableDictionary to the array and put a break point after it the dictionary is there, but then once I go to save it the array is empty with the description.
Post entire methods, not snippets. Where are you saving, where are you loading, and where are you doing this log?
Sorry about that. So what this is the method that creates the dictionary, adds it to the array, and then calls the saveFinalData: method. This is where I placed the first NSLog from above.
Code:
- (void) saveRunWithCalories:(NSNumber *)caloriesBurned distanceRun:(NSNumber *)distanceRun runTime:(NSNumber *)runTime runPoints:(NSArray *)runPts {
if ([info count] == 0) {
return;
}
NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithDictionary:[self.info objectAtIndex:[self.info count]-1]]; //Get the dictionary with the start date created earlier
[temp setValue:[NSDate date] forKey:@"endDate"];
[temp setValue:caloriesBurned forKey:@"caloriesBurned"];
[temp setValue:distanceRun forKey:@"distance"];
[temp setValue:runTime forKey:@"runTime"];
[temp setValue:runPts forKey:@"runPts"];
[self.info removeObjectAtIndex:[self.info count]-1]; // Remove the old empty version
[self.info addObject:temp];
NSLog(@"%@", [self.info description]);
[temp release];
[self saveFinalData];
}
The saveFinalData: method the following and where the second log is from
Well, there's nothing specifically in saveFinalData that is removing data. And your log is showing that the array is being emptied, not released. It would have (null) if the array was gone.
So, only a couple of possibilities that you'll have to chase down with more logs:
1. cancelRun is doing something to remove array contents
2. The other save method is removing something, which seems weird to me.
Yep you were right, cancelRun was getting called which removes the last object in the array. The issue was the [[WorkoutConfig]getInstance] isUserRunning] though - one of the variables wasn't getting set correctly.
But the array (now with the correct information) is still not being written to the plist file with:
I am using the simulator and I have the apps document directory open in Finder so I can see the file. If the file doesn't exist I create a new array and save that array to the plist with:
which works perfectly it creates the plist and I can see it in the documents folder. But afterwards when I try and save it once I have added a dictionary to the array nothing happens to the file and remains in its initial state from when I first created it
Update
I think I just figured out why it's not saving. In the dictionaries I am adding to the array, two of the fields are NSDate. So with:
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"runs.plist"];
if ([self.info writeToFile:path atomically:YES]) {
NSLog(@"Saved the array to plist file");
}
writeToFile:atomically: returns NO. I am going to have to save it as a NSString or NSData, but I'm guessing once that is done I won't have a problem with it.