Understanding The Data Model
By now you should know what an item is in the Liquid data model world. Most of what you interact with on the server using MC will be an item. When we talk about what a user is entitled to, it's always in terms of items. When dealing with this object composition on the client, this item representation is done with the class LDMItem. For the most part the LDPlatform framework does most of the work for you, but there are a couple of things that you'll need to get familiar with in order to interact with the server properly.
- A LDMItem is an NSObject
- It has two main components
- A Header (LDMItemHeader) which is equivalent to the "headers" portion of the item when looking at the JSON representation in MC
- A Data Dictionary, which is equivalent to the "Data" portion of the item when looking at the JSON representation in MC.
Most of the methods you'll be dealing with in the framework will often return you back result sets with LDMItem. If you are iterating through data sets, then instead of an array, we'll often give you something called LDMSearchResults. This is explained in another section, but simply understood is a container for LDMItems. To you, the developer, the LDMItem class should feel no different than a standard NSDictionary. There are two main methods to use when interacting with the data dictionary:
Get and Set
- (id) valueForKey:(NSString*) key;
- (void)setValue:(id)value forKey:(NSString *)key {
One retrieves the value for a given field name, while the other one allows you to set a new value. For example, consider the following schema:
{
"type": "Account",
"fields": [
{
"dataType": "refnum",
"name": "accountId
},
{
"dataType": "refnum",
"name": "accountName
}
]
}
To retrieve the account name for a given account, one would need to do the following:
LDMItem *testAccount = [[LDMDataManager sharedInstance] itemOfType:@"Account" withId:@"12345"];
NSString *accountName = [testAccount valueForKey:@"accountName"];
Similarly, you could update the account name for the same account with the following:
[testAccount setValue:@"New Account Change" forKey:@"accountName"];
[LDMDataManager sharedInstance] transactCreateOrUpdateWithItem:testAccount];
At any point you can also create a new account with:
LDMItem *newAccount = [[LDMDataManager sharedInstance] itemInstanceForTypeName:@"Account"];
//set values here
Remember, the item is only changed in memory unless you transact the item. In this case, calling transactCreateOrUpdateWithItem is what actually takes the item you modified in memory, place it in the local database, and eventually sends it to the server for final modification. This is covered in detail in another chapter.