I'm making an app with a list of the products of a company. The data does not need to be modified by the user. How can I save this information? The data must already exist locally when you download the app.
As per your requirement Plist or SQLite is good option for you.
Why Plist
Because it is lightweight.
Why SQLite
If you want to perform query on your data.
You can save the data in sqlite or coredata. For already filling the data in database you can use "sqlite manager" and run your queries in sqlite manager and save it on desktop(where you want). After create the filled database drag it into your project and do whatever your want. You have already filled database here.
You can go with either NSUserdefaults or CoreData, additionally there is a third party library called Realm.
Also check this question:
storing data locally on the iphone
Edit
The answer was provided by Sr.Richie in the link:
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"
Related
I recently found that when I save, for example, 10MB data into user defaults and I relaunch app, the app's memory is larger about 10MB than previous launch according to Xcode memory report.
So I can't use NSUserDefaults to save large data for a good performance?
And the data is email messages, have notion of folder (inbox, trash, etc) and the messages' attachments need save to local. I know SQLite and I use it to store data that need for search, but it's some complex, I don't know whether CoreData is a good choice.
I planned to store emails to NSUserDefaults because it's very simple for I just implement NSCoding protocol, but now it seems not good solution for the memory issue.
// Save
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mailFolders];
[userDefaults setObject:data forKey:#"myKey"];
[userDefaults synchronize];
// Read
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *mailData = [userDefaults objectForKey:#"myKey"];
NSArray *mailDataArray = [NSKeyedUnarchiver unarchiveObjectWithData:mailData];
NSLog(#"mail data size:%#", [NSByteCountFormatter stringFromByteCount:mailData.length countStyle:NSByteCountFormatterCountStyleFile]);
/**
mailFolder {
folderInfo,
messages
}
*/
i thnik there is no size limit to storing in NSUserDefaults.It's all upon device storage..
you can check this link
https://discussions.apple.com/thread/1763096?start=0&tstart=0
property lists should be used for data that consists primarily of strings and numbers. They are very inefficient when used with large blocks of binary data.
Tom,
There are many way in iOS to store the data.
1- NSUserDefault
2- Plist file
3- Data Base
If you have some small data then, NSUserDefaults and Plist is best option (no need to create database).
But if you have a large amount of data then, i would suggest you to use a proper DataBase (Sqlite OR CoreData).
NSUserDefaults is really meant for storing small pieces of data such as settings, preferences, and individual values.
Suggest using Core Data or SQLite to store a large list of elements.
There was a very good question about sqlite vs. Core Data;
Working with data in iOS Apps and Core Data vs SQLite 3 on Stack Overflow, you may want to read through the answers to that question.
I want to fill a tableView with some data (Strings), that I save in another View/ViewController.
My viewDidLoad of the SaveViewController:
NSString *savestring = #"Test: This is a test!";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:#"savedstring"];
[defaults synchronize];
Is this a good way to save data? (It's very easy so I am using it at the moment)
Now I am having another View (TableView), that should fill with this string dynamically. (I want to add the date and some string, it should be something like a training journal)
2 . How can I do this? Should I change my "saving" ?
No NSUserDefaults is not a good place to save data with the exception of storing a few user preferences. You should look into using Core Data for your general storage needs.
You can also save to plists which depending on your data can be simpler e.g. you just need to store a single dictionary.
You can sore this changes in some model object. Than you can send this object using delegate, or u can create singleton app manager with general model object. If you want to save this data to database, you should read about CoreData.
I am new to cObject or App world, I'm about to make an app where I need to store some information on some players (football), but I do not know where to store them and will hear what you think is best ?
I need to store data like name, age, etc.. I know how I'm doing some text boxes so I can enter the data, but do not know where to save it, I can read that I can use the plist and SQLite, so my quest is if I have about 16-20 names and should only used em on the device where the data have been saved and where I can edit and deleted them, what would you recommend to use as a data source ! or would you recommend another approach ?
Im coding in Xcode atm.
I would suggest you to use CoreDataModel. CoreDataModel is a data storage model for Objective C. It's the way faster then SQL databases and similar to in terms of creating tables, making table connections. You can find more information in Ray Wenderlich's Tutorial.
Since you data model would include many attributes (name, age, etc.), or even relational models I would not suggest using plists in your case.
you can use NSUserDefaults if the data you want to store is very small.
Lets say you have an array of users
NSArray *users = ,,,,,;
to save it
[[NSUSerDefaults standardUserDefaults] setObject:users forKey:#"myKey"];
For later to get users data,
NSARray *users = [[NSUSerDefaults standardUserDefaults] objectForKey:#"myKey"];
Hello I can not save locally where I have to save an array of strings, I add that this array must be saved when closing the application for iOS and the reopening needs to be recharged, I found thousands of guides online but none are very specific, someone says use NSUserDefaults others say to use NSCoding but in both cases i can not recharge these data can anyone help me?
To Save SMALL data - NSUserDefaults, NSCoding as suggested by other people or saving in plist also or else create a file in document directory and write the contents of array in that file.
LARGE Data- Save it in database using either sqlite or core data.
Choice depends on the requirement of the application and since you have not given the details of the data like maximum number of strings in the array, everyone can suggest you instead of telling a solution. Hope it helps :)
You can use NSUserDefaults:
[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:#"Key"];
To get the value from NSMutableArray:
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"Key"]];
Hope this is helpful.
What kind of database do you suggest? I want to store user email, username, password, and a couple other random pieces of information. It doesn't have to be fancy. Just a simple database. Are there any free options?
The user information needs to be stored in the keychain to keep it secure.
Any other information could be stored in any one of:
User defaults NSUserDefaults
File on disk (maybe a plist)
Database Core Data (technically just a file on disk)
Which you choose depends on what the data is, how much there is and what kind of access you need to it.
If your data is small and chosen by the user as some kind of setting then user defaults makes sense and is the lowest cost for you to implement.
To use a database, check out Core Data intro.
Wain is right but I think as you want to store small amount of data for further use, the most efficient ways is to use NSUserDefault.
NSUserDefault stores data in NSDictionary type things.
I think this is the step you have to take:
1- check if data exists. I mean if user selected the number if the last run of your app. So in viewDidLoad method:
NSMutableDictionary *userDefaultDataDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if (userDefaultDataDictionary) {
// so the dictionary exists, which means user has entered the number in previous app run
// and you can read it from the NSDictionaty:
if(userDefaultDataDictionary[LABLE_KEY]){
//and store it
}
}
2 - you can implement some method like syncronize to store data in NSUserDefault every time something has been changed.
- (void) synchronize
{
NSMutableDictionary *dictionaryForUserDefault = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if(!dictionaryForUserDefault)
dictionaryForUserDefault = [[NSMutableDictionary alloc] init];
dictionaryForUserDefault[LABLE_KEY] = //data you want to store
[[NSUserDefaults standardUserDefaults] setObject:dictionaryForUserDefault forKey:ALL_DATA_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
P.S. and don't forget to #define your keys for your dictionary:
#define LABLE_KEY #"Lables"
#define ALL_DATA_KEY #"AllData"
Store it in a plist. If you're talking about data pertaining to one or a few users, that's probably the easy thing. here is a simple example.
Since you say database, store in Sqlite. There's some provided stuff for it already in xcode.
The entire database is contained in one file, which can be moved around if you need to.
Here is some more information on how to use one in your app.