Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
kenichi_unnai
Advisor
Advisor

Now that we have the SODataEntitySet instance, we pick up each entity out of the entityset.

01  id<SODataEntity> entity = [entityset.entities objectAtIndex:row];

Obtaining the value out of the entity is demonstrated here:

01  NSString *propValue = (NSString *)[(id<SODataProperty>)entity.properties[propName] value];

Question - how do we pick up the value out of the complex type? - Here's the answer.

01  NSDictionary *details = (NSDictionary *)[(id<SODataProperty>)entity.properties[detailsName] value];
02  NSString *propValue = (NSString *) [(id<SODataProperty>)details[propName] value];

So the complex type gets returned as the NSDictionary object.

Okie, so far we have done pretty much about Read operation. Let's move on to the CUD scenario.

First thing we need to understand is how we construct an entity. Here's the code snippet:

01  NSMutableArray *properties = [NSMutableArray array];
02  id<SODataProperty> prop = [[SODataPropertyDefault alloc] initWithName:@"myKey1"];
03  prop.value = @"My prop value";
04  [properties addObject:prop];
05  prop = [[SODataPropertyDefault alloc] initWithName:@"myKey2"];
06  prop.value = @"Another prop value";
07  [properties addObject:prop];
08  // goes on and on..
09  SODataEntityDefault *entity = [[SODataEntityDefault alloc] initWithType:@"MyEntity"];
10  for (id<SODataProperty> prop in properties) {
11    [entity.properties setObject:prop forKey:prop.name];
12  }

#02, #03, and #04 are creating key/value pair of each property. We'll repeat until we set all the property values for the entity. And #09, #10, 11, and #12 is iterating through to set the SODataEntity properties.


Note about #09 - the snippet uses an entity name as "MyEntity", but in an actual case, you need to qualify the entity type with the full namespace such as "RMTSAMPLEFLIGHT.Booking".

Now that you got an idea about how to work with an entity object, let's take a look at the CUD methods. As you have understood the Read operation, they should be very straightforward.


Create operation (= HTTP POST)

01  [onlineStore scheduleCreateEntity:entity collectionPath:@"CollectionName" delegate:self options:nil];

Update operation (= HTTP PUT)

01  [onlineStore scheduleUpdateEntity:entity delegate:self options:nil];

Delete operation (= HTTP DELETE)

01  [onlineStore scheduleDeleteEntity:entity delegate:self options:nil];

One coding remark about Update and Delete. Before invocation of the methods, make sure you set ResourcePath for the entity. The url value would be something like "TravelagencyCollection('00000105')" - something you can identify the entity in OData convention. In most case the setResourcePath: and editResourcePath: should set the same value but it depends on how your OData service is implemented.


Tip: If you already get the entity with the store, the entity should already contain these resourcePaths.

01  [entity setResourcePath:url editResourcePath:url];

And - Read operation (= HTTP GET)

01  // if you want to refresh/reload an entity
02  [onlineStore scheduleReadEntity:entity delegate:self options:nil];
03  // or..specify the resource path
04  [onlineStore scheduleReadEntityWithResourcePath:resourcePath delegate:self options:nil];

That's pretty much all I want to share about the online scenario. You can be confident you got a solid fundamental about how OData online API works. Next blog will introduce you the onboarding API. I know - everyone is interested in offline API, but let me quickly cover the onboarding first.



See you in the next blog,

Ken


List of blogs

1 Comment