Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
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.
{type = mutable-small, count = 0, values = ()}
Do you have any idea what would be causing that if I am using
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.
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
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:
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.
Replies
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeWhen 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.
Do you have any idea what would be causing that if I am using
to initialize the array?.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAfter adding the dictionary to the array I get the following: After I have added the object and call my save method the same NSLog gives:
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- (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];
[COLOR=\"Red\"]NSLog(@\"%@\", [self.info description]);[/COLOR]
[temp release];
[self saveFinalData];
}
The saveFinalData: method the following and where the second log is from
- (void) saveFinalData {
if ([[WorkoutConfig getInstance] isUserRunning]) {
[self cancelRun];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@\"runs.plist\"];
[self.info writeToFile:path atomically:YES];
[COLOR=\"red\"]NSLog(@\"%@\",[self.info description]);[/COLOR]
hasActiveSession = NO;
}
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSo, 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.
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeBut 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:
if (self.info == nil) {self.info = [[NSMutableArray alloc]init];
[self.info writeToFile:path atomically:YES];
}
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:
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.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome