This page show some basic examples of usage for the JUMP Database Module.
On our examples we presume that you have your Core Data models created. See Introduction to Core Data Programming Guide
to learn more about it.
The first step to start to work with the Database Manager is initialize the all system.
The above code perform a lot of tasks. He initiate the full Core Data model and create the persistent stores if needed. We're using an Singleton Instance of the Manager. You always can initiate one regular instance, but in the most common cases you will use this way.
Database operations isn't called directy to the manager. Instead we create one instance of the JPDBManagerAction class, configure the data and settings to perform some CRUD operation and finally run this action. Yes, looks like a lot of work, but actually is not. Let's see some examples:
id newRecord = [JPDatabaseManager createNewRecordForEntity:@"MyEntity"];
This is was really simple, isn't? Let's see this same operation on a more custom fashion:
JPDBManagerAction *anAction = [[JPDBManagerSingleton sharedInstance] getDatabaseAction]; [anAction setCommitTransaction:YES]; id newRecord = [anAction createNewRecordForEntity:@"MyEntity"];
Well this looks a litle bit more complicated. Let's understand this two processes: The first one uses the JPDatabaseManager convenient macro shortcut that returns an JPDBManagerAction instance and perform this method in only one pass, is pure convenience. This macro is declared in the JPDBManagerDefinitions.h file.
The second way needs more steps, but you should use that way when you need more control of the all process, for example, you can configure if you want to automatically commit the operation or create some more dinamically code on your database operations.
Core Data never perform operations directly to the Persistent Store. It maintains one memory model and you need to save (commit) your changes. You can configure the Database Manager to automatically commit every operation as default and also you can configure each Database Action case by case.
[[JPDBManagerSingleton sharedInstance] setAutomaticallyCommit:YES];
Now all Database Action will be committed immediattelly. This is means that every Database Action instance returned by the getDatabaseAction: method will be configured with this setting. Now you can use the approach showed in the Performing Database Actions section to configure some specific action with different setting.
To manually commit all pending operations to the Persistent Store is very simple:
If fore some reason the commit operation fails one NSNotificaton is posted. See Handling Errors for more inforation.
You will enconter a huge number of convenient methods to perform simple and very complex queries to the database. See Performing Queries for more information. Here are some examples of some query operations:
// Query all data from 'MyEntity'. NSArray *allData = [JPDatabaseManager queryAllDataFromEntity:@"MyEntity"]; // Query data using the 'MyFilter" Fetch Template and ordering by 'id'. NSArray *allData = [JPDatabaseManager queryEntity:@"MyEntity" withFetchTemplate:@"MyFilter" orderWithKey:@"id"];
You can also mount your Database Actions on a more custom fashion:
JPDBManagerAction *anAction = [[JPDBManagerSingleton sharedInstance] getDatabaseAction]; // Apply the Entity. [anAction applyEntity:@"MyEntity"]; // Limit to 20 rows. [anAction setStartFetchInLine:0 setLimitFetchResults:20]; // Order by 'name' in desceding order. [[anAction applyOrderKey:@"id"] setAscendingOrder:NO]; // Perform the action. NSArray *first20rows = [anAction runAction];
There you go, the Database Action is very flexible to configure and also very convenient to use. Note that all apply... methods return the self instance. So you can nest many apply methods on the same line.