1

On viewDidLoad, my application populates the NSMutableArray arrayOfHaiku, which is an array of NSDictionary items, as follows:

 NSError *error;
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *path = [documentsDirectory stringByAppendingPathComponent:@"haiku.plist"];
 NSFileManager *fileManager = [NSFileManager defaultManager];
 if (![fileManager fileExistsAtPath: path])
 {
 NSString *bundle = [[NSBundle mainBundle] pathForResource:@"haiku" ofType:@"plist"];
 [fileManager copyItemAtPath:bundle toPath: path error:&error];
 }
 self.arrayOfHaiku = [[NSMutableArray alloc] initWithContentsOfFile: path];

While using the application, the user adds items to self.arrayOfHaiku. As of now I include the following code in viewDidUnload:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"haiku.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: path])
{
    [self.arrayOfHaiku writeToFile:path atomically:YES];
}

Here are my questions:

  1. Does my code for viewDidUnload a) simply write over the document storing the array in the Documents folder (that is, the array accessed in viewDidLoad) with the new, expanded array (which is what I want to do), or does it b) append the new, expanded array to the array already stored in the Documents folder so that I end up with an array that's more than twice as long with a bunch of repeats?

  2. If the answer is "b)", what do I need to change to do "a)"?

  3. Is it better a) to do it the way I'm doing it, at viewDidUnload, or b) to expand the array in the documents folder each time the user adds an item to self.arrayOfHaiku?

  4. If the answer is "b)", what do I need to change to do "b)"?

Thanks for your help.

1 Answer 1

2
  1. If the file exists, it will be overwritten, else it will be created (option a).

  2. It all depends on the number of records. If you have a huge number then rewriting them everytime could be expensive. Otherwise you have to look to another solution. I honestly would not suggest using viewDidUnload because it might not fire in certain circumstances for example, if the app is backgrounded.

  3. Unfortunately, there is no ready function for 'updating' a plist with dictionary objects like writeToFile: . You can use NSFileHandler's fileHandleForUpdatingAtPath: function but then you would have to change the plist file manually (Which i dont think is a simple process).

I personally would suggest using Core Data unless your data structure is very simple and 'option a' would not be an expensive process.

hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so very, very much! The data structure is very simple (self.arrayOfHaiku is an array of NSDictionary items each one of which contains exactly two objects and two keys) and self.arrayOfHaiku is unlikely ever to be contain than 200 or 300 items, so I'll try it in a few places--see how much it costs to do it every time, or whether it makes a difference putting it elsewhere--but I hadn't thought about that issue with viewDidUnload so in any case I'll find a new home for it. Thank you so very, very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.