Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-27-2011, 03:24 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: MA
Posts: 126
nd049 is on a distinguished road
Default Loading/Saving plist files

Hello Everyone,

I am trying to save an NSMutableArray made up of NSMutableDictionaries with the following code:

Code:
//LOADING FROM PLIST
 NSString *path = [[NSBundle mainBundle] bundlePath];
        NSString *finalPath = [path stringByAppendingPathComponent:@"runs.plist"];
        
        NSMutableArray *temp = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];
        self.info = temp;
        [temp release];

// SAVING TO PLIST
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"runs.plist"];
    [info writeToFile:path atomically:YES];
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!
nd049 is offline   Reply With Quote
Old 10-27-2011, 03:33 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Looks like you're dealing with 2 different locations.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-28-2011, 11:57 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: MA
Posts: 126
nd049 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
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.

Quote:
<CFArray 0x87c51b0 [0x2254b38]>{type = mutable-small, count = 0, values = ()}
Do you have any idea what would be causing that if I am using

Code:
NSMutableArray *temp = [[NSMutableArray alloc] initWithContentsOfFile:path];
    self.info = temp;
    [temp release];
to initialize the array?.
nd049 is offline   Reply With Quote
Old 10-28-2011, 11:59 AM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Use logs, not breakpoints.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-28-2011, 12:09 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: MA
Posts: 126
nd049 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Use logs, not breakpoints.
Okay, for these next logs I used
Code:
NSLog(@"%@",[self.info description]);
After adding the dictionary to the array I get the following:

Quote:
WorkoutTracker[24749:17003] (
{
caloriesBurned = 72;
distance = "-0.000521371192";
endDate = "2011-10-28 17:04:47 +0000";
runPts = (
"<+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/28/11 1:00:35 PM Eastern Daylight Time"
);
runTime = "7.065720021724701";
startDate = "2011-10-28 17:04:39 +0000";
}
)
After I have added the object and call my save method the same NSLog gives:

Quote:
WorkoutTracker[24749:17003] (
)
nd049 is offline   Reply With Quote
Old 10-28-2011, 12:11 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Post entire methods, not snippets. Where are you saving, where are you loading, and where are you doing this log?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-28-2011, 12:21 PM   #7 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: MA
Posts: 126
nd049 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
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
Code:
- (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];
    NSLog(@"%@",[self.info description]);
    
    hasActiveSession = NO;
}
nd049 is offline   Reply With Quote
Old 10-28-2011, 12:41 PM   #8 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-28-2011, 05:38 PM   #9 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: MA
Posts: 126
nd049 is on a distinguished road
Default

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:

Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"runs.plist"];
    [self.info writeToFile:path atomically:YES];
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:

Code:
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

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array/>
</plist>

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.

Last edited by nd049; 10-28-2011 at 05:53 PM.
nd049 is offline   Reply With Quote
Old 10-28-2011, 08:50 PM   #10 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You can save NSDates. There must be some other issue.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-28-2011, 10:30 PM   #11 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: MA
Posts: 126
nd049 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
You can save NSDates. There must be some other issue.
Ohhhh it's the Array of CLLocations that is in the dictionary. Saved it as NSData and its all working now. Thanks Brian for all of your help!
nd049 is offline   Reply With Quote
Reply

Bookmarks

Tags
nsarray, nsdictionary, plist

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 388
13 members and 375 guests
13dario13, 7twenty7, buggen, EvilElf, glenn_sayers, j.b.rajesh@gmail.com, LunarMoon, morterbaher, QuantumDoja, sacha1996, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:30 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0