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.
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 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"
I need to pre populate my core data base with only one Managed Object.
Currently i'm checking on AppDelegate if it's the fisrt time that the app runs, and then a add the object, like this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL firstTime = [defaults boolForKey:#"firstTime"];
if (firstTime) {
[dataManager insertManagedObject:myManagedObect];
[defaults setBool:NO forKey:#"firstTime"];
[defaults synchronize];
}
insertManagedObject method checks if the managedObject is already in the data base.
It's working fine, but i'm afraid in future app updates this could cause me some kind of trouble, mainly if i change my data model and add a new data model version.
What's the best approach to do that?
Why don't you execute a fetch request a see if the store already
contains that managed object? e.g setting a specific identifier for
that managed object...
Following my comment you could just set up a fetch request against your entity and see if the store already has an instance for it.
This is simple enough to achieve.
If you need to query against a specific object, you could set a property identifier (i.e. a guid) for your entity and use a predicate to see if the object with the specific guid exists or not.
If you share some other details I can give you other suggestions...
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 came cross this code as shown below. how could I save existing annotation pins info to NSUserdefault without creating any buttons(IBAction)? Should I put NSUserDefault code into viewWillDisappear? Is that the right way to do it?
To save:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setDouble:location.latitude forKey:#"savedCoordinate-latitude"];
[ud setDouble:location.longitude forKey:#"savedCoordinate-longitude"];
[ud setBool:YES forKey:#"savedCoordinate-exists"];
[ud synchronize];
viewWillDisappear is one moment that is often used to save state, but it is not the only place or the only possible place. What if the user suspends your app? You won't get viewWillDisappear. What if viewWillDisappear is not a place where you have access to the annotation information? Perhaps it would better to keep saving info to user defaults as the annotations are created. It depends on the nature and purpose and architecture of your app; it's a problem for you to solve. Your job is to know when your code will run under the event-driven framework, and behave appropriately.
You can put that code wherever you want, when you want to save that data. NSUserDefaults is accessible anywhere. Synchronize is what saves it to disk.