views:

50

answers:

2

Hi

I am building an add-on to my app where the user can search for an item in a list that is pre-populated with data from a .plist file. It is an NSDictionary. If the term, the user searched for, does not exist, the user can tap a + button and add it so it is there the next time.

First of I thought it would be as easy as using the NSUserDefaults, but a few problems arises.

To have the list included I must place it in the bundle, but if it is there I can not add new key/value pairs to it. This I can only do with files situated in the Documents folder.

So I guess I have to bundle the plist, then on first run I'll move it to the documents folder and access it there.

This opens up the problem when I need to update the app, I guess it will overwrite the values the user put in.

Is there a secure, easy-understandable, right way to achieve the functionality I describe?

Thanks for any help given:)

Edit: **** the actual approach, as suggested by TheSquad and TomH *****

+ (NSMutableDictionary*) genericProducts {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"GenericProducts.plist"];

    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];  
    NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"GenericProducts.plist"];


    if([fileManager fileExistsAtPath:documentPlistPath]){

        NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
        return documentDict;

    } else {

        NSError *error;
        BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];

    if (success) {
        NSMutableDictionary *newlySavedDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
        return newlySavedDict;
    }

    return nil;
}

}

And for adding a new product to the list:

+ (void) addItemToGenericProducts:(NSString*) newProduct {

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"GenericProducts.plist"];
    NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
    [documentDict setObject:newProduct forKey:[MD5Checksum cheksum:newProduct]];

    [documentDict writeToFile:documentPlistPath atomically:YES];

}

+2  A: 

I had the same thoughts with my sqlite database...

I end up doing exactly that, copy the bundled file into documents in order to be able to modify it.

What I have done is checking at each startup if the file exist, if it does not, copy it. If you do an update of your App, the documents folder will not be touch, this means the copied file from the previous version will still be present.

The only issue is that if you want your plist to be upgraded you will have to handle that in your application. If you have to do so I suggest you use the NSUserDefault to check if a previous version of the app existed before...

TheSquad
Thanks TheSquad:) I put together as rough cut of the code and put it in my answer. I works just fine. I guess I should never overwrite the plist as this would lead to me having to merge the things the user added with the things I added, synching I guess. Lots of work for an edge-case. Maybe I'll do some time-stamping in the future.
RickiG
Try updating your file in document the less you can, it is a real pain to do so, believe me ! I got bad updates because of that, and of course bad reviews coming along with it... Now I keep an exact copy of each version I have released in order to test the update from one version to another, I suggest you do the same, or you'll regret it :-)
TheSquad
I would be so much easier if I knew what I would put into the plist in the future:)
RickiG
+2  A: 

The contents of the documents directory is not altered when an application is updated.

The contents of the documents directory are deleted when the user deletes the app.

When the app is run the first time write a flag to NSUserDefaults. On subsequent runs of the app, check for existence of the flag. (alternatively, you can just check for existence of the plist in he documents directory)

TomH
Thanks Tom:) I went with your suggestion on testing if the file is found in the documents folder. It is an action that is seldom called, only in the edge case where a user goes to do something outside of the normal use. So the little overhead that comes from testing if the file exists is weighted up by the flexibility.
RickiG