Reputation: 2520
I want to save some strings from different ViewControllers to ONE plist. At the moment the viewDidLoad of my VC1 save the string to the plist and when I call the VC2 then my plist has be overwritten with the string from VC2.
My viewDidLoad in VC1 and VC2 is equal and like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSArray *array = [NSArray arrayWithObjects:dateString , nil ];
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [path objectAtIndex:0];
NSString *filePath = [documentFolder stringByAppendingFormat:@"list.plist"];
//save to array
[array writeToFile:filePath atomically:YES];
NSLog(@"file Stored at %@",filePath);
Questions:
How can I handle this, that the pointer in my plist goes to the second position so that I am not overwriting my old data?
I only want to save Strings (dates and some text) from three different ViewController is a plist a good way to do this or should I use core data?
Upvotes: 0
Views: 101
Reputation: 1141
2 ways of solving first issue:
Instead of creating a local version of array which you are overwriting every time, why don't you use an NSArray that is static and global and visible to both Controllers such that each one appends to the same object and therefore they don't step on each other's toes.
Instead of write to file .. append to file.
NSString *contents = [NSString stringWithContentsOfFile:filepath];
contents = [contents stringByAppendingString:textToWrite];
[contents writeToFile:filepath atomically:YES encoding: NSUnicodeStringEncoding error:&err];
For answer to your second question.. I would say it depends on the kind and volume of the data interactions.. if the data is small and interactions are far and few then file storage is fine.
If the data is large and interactions start to become more involved and complex.. with multiple classes reading and writing, CoreData is definitely an option to look at.
My 2 cents worth
Upvotes: 1
Reputation: 2447
I suggest you to use core data It's really simple and can avoid you code to manage the saving, dates and strange logics. It also provide you useful features like:
To avoid the boilerplate code related to core data try to give a look to magicalrecord
Upvotes: 1