I am new in share preference so kindly forgive me this silly question. I am developing an application in which I have to fetch data from xml and all data are in text form, So I think I'll store this all data into shared preference because this is quite light weighted and not too much so thats why not going for sqlite. So before jumping into this approach I want to know is that good approach or not and what are recommendations on storing data in share preference?
To store data:
NSString *someValue = #"someval";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:someValue forKey:#"nameOfSomeValue"];
[defaults synchronize];
To retrieve it later:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *someValue = [defaults valueForKey:#"nameOfSomeValue"];
SharedPreferences is an Android thing, and your question seems to be about iOS.
You can store user preferences and other lightweight data in NSUserDefaults on iOS, as described in my answer here.
If you want to store data in XML files, this answer provides a helpful example.
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'm developing an app that gets identification number and age at the first launch.
I also want to use these values later on. Where should I save these values in?
Is it automatically saved?
My language is Swift.
You can save them in NSUserDefaults,
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:#"value:#"key"];
[def synchronize];
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 used NSDefault in my app to backup some images and got rejected because it uses 6mb of storage.
Can anyone help me add the donotbackup attribute into it? I would like to keep userdefault directory if possible so old users don't lose their images. Any help would be really appreciated :)
My current code is:
to save:
- (IBAction)d1p:(id)sender {
lbl1.text=txt1.text; [txt1 setAlpha:0];
NSString *savestringln1 = lbl1.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestringln1 forKey:#"savedstringlbl1"];[defaults synchronize];
[self.view endEditing:YES];
}
To load:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstringlbl1 = [defaults objectForKey:#"savedstringlbl1"];
So you want to back up images? NSUserDefaults isn't really meant for that.
Why not try adding iCloud support instead?
The only way to stop something from not being stored in UserDefaults is to just not put it in User Defaults, I am not sure what you mean by backup, you are just duplicating images on the device in two different formats. If the image is on your file system you can store the path to the image in User Defaults if thats useful to you. If you are trying to prevent unintentional deletion can you just mark the 'deleted' images has hidden from the user in some way, a flag, moved to a different location perhaps. You also might want to look into CoreData that can deal with larger amounts of data ad do things like have non serialised properties and such, but that is still just storing your data on the device.
I am new to iOS programming - I have written an iOS app for a company that uses the app for their workers to log in when they go to work, and to log out when they leave. (They often work on remote places).
All I need now is a way to store two simple strings.
1. String is the user name
2. String is a salted md5 password
Then the user doesn't need to write all the credentials every time he wants to login and out.
What is the simples? SQLite, CoreData, Plst?
Thanks!
See my answer here:
I'd avoid using a plist. The easiest way to save simple data in an application, by far, is NSUserDefaults.
Check out this tutorial for a simple guide on how to use NSUserDefaults. Always be sure to synchronize NSUserDefaults when you're done writing to them.
Example:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// Storing an NSString:
NSString *user = #"MyUsername";
[prefs setObject:user forKey:#"username"];
[prefs synchronize];
Later:
// Retrieving an NSString:
NSString *savedUserName = [prefs stringForKey:#"username"];