antonio escudero
antonio escudero

Reputation: 109

data won't be deleted in a uitableview after updating the app

I have been reading so many topics here about this, but I could not have found it yet.

I want to have a UITableView where I can put some dictionaries and inside them, have words with some fields (about 4). So far it is ok, but my question is what is the best way to don't erase the data after upgrading the app to a new version. I mean, when an user upgrade the app, all those words/dictionaries won't be deleted because of the update.

I don't know if I should use a NSMutableArray of class of words (for example), or use Core Data, or SQLite.

Any help would be very appreciated. Thanks in advance.

Upvotes: 2

Views: 110

Answers (2)

Peter Sarnowski
Peter Sarnowski

Reputation: 11970

If you have dictionaries that are in fact NSDictionaries, serializing them using NSCoding protocol to a file inside of the documents is easy, fast and reliable. The contents of the Documents directory remain unchanged if you do an upgrade of the app.

This is the code I use in one of my apps (it saves a list of quotes that has been favorited by the user)

- (BOOL) saveQuotesToDisk
{
    //get path to events list    
    NSArray * dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * fullpath = [NSString stringWithFormat:@"%@/%@", [dirPaths objectAtIndex:0], @"FavQuoteList.plist"];

    //create data to be saved
    NSMutableDictionary * rootObject = [NSMutableDictionary dictionary];
    [rootObject setValue: _SavedQuotes forKey:@"_SavedQuotes"];

    //write data
    bool success = [NSKeyedArchiver archiveRootObject: rootObject
                                               toFile: fullpath];

    if (success) NSLog(@"Quotes list saved.");
    else NSLog(@"ERROR on saving quotes list!");

    return success;
}

- (BOOL) readFavoriteQuotesFromDisk
{
    //get path to quote list    
    NSArray * dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * fullpath = [NSString stringWithFormat:@"%@/%@", [dirPaths objectAtIndex:0], @"FavQuoteList.plist"];

    //read data
    NSDictionary * rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile: fullpath ];
    _SavedQuotes = [[rootObject valueForKey:@"_SavedQuotes"] retain];

    //check data
    if (_SavedQuotes == nil) return NO;
    else return YES;
}

You can add more objects to the rootObject as long as they conform to NSCoding Protocol.

Upvotes: 1

Saurabh
Saurabh

Reputation: 22883

Well I think you can store that data in NSUserDefaults or in Documents directory.. Documents directory's content don't get deleted when you update..

Upvotes: 0

Related Questions