random
random

Reputation: 8608

Core Data over using?

I have an app that tracks a users driving trips (Paths). I save all the information using Core Data.

The db structure:

Path ->> Point

Point contains lat and long values.

What I do is, each time CLLocationManager is updated I add that point to an array. Once the user has reached the end of the path, I loop through and add all those locations to the db.

My question is...is this the best way to go about this? My two options are:

  1. Add all locations to array, then add all locations to core data.

  2. Each time CLLocationManager is updated, add that directly to core data.

I'm not sure if there is some best practice to accessing/altering core data. Should I do it in bulk (for loop), so that I can call

if ([managedObjectContext save:&error]) {
   // handle save error
}

at the end of the for loop and keep it all condensed.

Or should I simply add a new Point each time CLLocationManager updates calling [managedObjectContext save:&error] after each update.

The only concern I have with Option1 is that if the app crashes while recording a path, none of the information will be saved.

So a benefit of using Option2 is that the data will be saved after each update but I'm not sure if accessing core data this frequent is bad practice.

Thank you very much for taking the time to help.

Upvotes: 0

Views: 122

Answers (1)

Mundi
Mundi

Reputation: 80265

With the assumed frequency of NSLocationManager updates (max every few seconds) it is absolutely fine to save frequently. Also, your array will eat up more and more memory which is not really necessary.

You could still do it in discreet quantities, say, one save every 10 points.

Also you should perhaps save in applicationWillResignActive in case the app is interrupted.

Upvotes: 1

Related Questions