Core Data - create sample data when application is installed - ios

I develop iOS app, which is using Core Data and I would like to create same sample data records when the app is installed. Is there any easy way how to do it..?
For example there is entity "Employee" and I would like to add "John Doe" employee when the app is installed.
Can someone help me with that?
Thanks

Is a pre-populated database for coredata what you want ? If so, I think you should find this link interesting : Any way to pre populate core data?
Otherwise, you can maybe save in user defaults a boolean to know if it is the first time that the application has been launched. If this is the first time, you write some code to add the objects you want.

Thanks Sunder for you answer.
In the end I decided for fast solution by checking whether the app is running first time in the AppDelegate didFinishLaunching method:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (![userDefaults valueForKey:#"PCAppDelegateFirstRun"] )
{
[PCDatabase createSampleData];
[userDefaults setBool:YES forKey:#"PCAppDelegateFirstRun"];
}

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"

Collect user input from UITextField and storing it in a certain place - Xcode

I can't figure out how to make the create an account function work in my application... I have my login system set up like so: http://imgur.com/dKGRnok The first set of strings are the passwords. The second set are the usernames. For someone to create an account that allows them to login, I have to make a string manually through the application. How can I make this function so it gets their input and places it as a string as long as the same username doesn't already exist? http://imgur.com/lVzAV8i
You need to create a local database, the proper way would be to use either sqllite or core data to create your structure, then simply add/remove users.
For an sql tutorial for ios follow this:
http://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app
Or go to this site and check the section that says "Saving and Loading Data"
http://www.raywenderlich.com/tutorials
I personally prefer core data because its the most complete solution once you get used to setting it up.
If what you are asking is a persistent data, [[NSUserDefaults standardUserDefaults] setValue: forKey:] will be the answer.
Once you save a data with NSUserDefaults, the data will be persistent. You can get the value with [[NSUserDefaults standardUserDefaults] valueForKey:]
Added
I misunderstood your question. In this case you should use local database as Chiques said.
Core Data will be the answer.
http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started

Pre populate core data with only one Managed Object

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

iOS: What are the Advantages / Disadvantages of using plist or SQLite, as the data source

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"];

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.

Resources