Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing data locally on the iphone

I am building an app where I'd like to store user information locally on the device, without using any server database - but everything on the device side. I am looking to store specific user locations and show it in a table view, and so even when the user launches the app later - I can pull the history and feed the history table with the past locations. Basically a read/write capability from a local database.

Now, I know that there were many questions like that before but I could not find one that addresses saving data locally without an external data base. I am not sure, for example, that using Core Data is the correct and simplest thing to do here.

Would appreciate any advice on that.

like image 300
TommyG Avatar asked Sep 06 '25 14:09

TommyG


2 Answers

For simple data you should use NSUserDefaults. CoreData is very cool but mainly to store DB structures, and introduces complexity (but i love it:)). If you just need to store String, Array and so on (basically prefs), you can go with NSUserDefaults:

For example:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];  //load NSUserDefaults
NSArray *fakeFavs = [[ NSArray alloc] initWithObjects:@"2",@"4", @"100", nil];  //declare array to be stored in NSUserDefaults
[prefs setObject:fakeFavs forKey:@"favourites"];  //set the prev Array for key value "favourites"
like image 123
Sr.Richie Avatar answered Sep 09 '25 18:09

Sr.Richie


You have basically two options to store data:

  1. CoreData (if you are planning to use newer versions of iOS)
  2. SQLite (supports any version of the SDK)

CoreData uses SQLite, its API is a little bit easier to use (you don't need to know SQL or write a lot of functions to read and write your data).

SQLite API is still a great choice, since it uses the C API for SQLite, which is very well documented and straightforward to use. It has the benefit that you can target older iOS platforms with this.

With both options, data will be stored client side, and will be backed every time user syncs his/her phone with iTunes.

like image 32
Pablo Santa Cruz Avatar answered Sep 09 '25 18:09

Pablo Santa Cruz