Pre populate core data with only one Managed Object - ios

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...

Related

iOS Store Data Locally

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"

How to fill TableView with data from another ViewController

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.

Directly add to array in defaults

Is it okay to call this?
[[[NSUserDefaults standardUserDefaults] objectForKey:#"searchEnginesOrder"] addObject:#"eBay"];
I have a UITableView whose cells I want to load directly from the defaults, and I also want to modify those defaults. I'm wondering if the above line actually has any effect (when I NSLog the default's array searchEngineOrder it's null, I'm wondering if it's because the above code isn't actually adding to the defaults.
Generally speaking, that line of code would crash. User defaults returns immutable objects so calling addObject: will throw an exception.
You also shouldn't rely on changes made to returned objects being backed into the data store - this isn't user defaults specific, it goes for any API which doesn't document it as supported.
You should be separating your logic between your working data and your stored data with defined modifications and save points. Ensure that you mutable copy the data you extract from user defaults. You should also use registerDefaults: to setup initial values so you don't need to check for existence.
NSUserDefaults returns immutable objects. You have to create mutable copies in order to edit the values.
NSMutableDictionary *subSettings;
subSettings = [[[NSUserDefaults standardUserDefaults]
objectForKey:YOUR_SUBMAP_KEY] mutableCopy];
[subSettings setObject:YOUR_NEW_VALUE forKey:YOUR_VALUE_KEY];
[[NSUserDefaults standardUserDefaults]
setObject:subSettings forKey:YOUR_SUBMAP_KEY];
[subSettings release];

Best way to store user information for my iOS app

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.

Where to put NSUserDefaults to Store Annotations

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.

Resources