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

Let's read the entityset.


01  [onlineStore scheduleReadEntitySet:oDataQuery
02                            delegate:self
03                             options:nil];

The oDataQuery is OData query string such as "CarrierCollection?$top=10..", something you should be already familiar with as the OData rule in general.


The delegate: methods requires another new delegate protocol named SODataRequestDelegate. This delegate has two mandatory callback methods:

  • requestServerResponse:    HTTP 2**, give back the result payload.
  • requestFailed:    HTTP 4** or 5**, give back an ODataError when the server replied with it.

And three optional callback methods:

  • requestStarted:    called when the OData request execution starts.
  • requestFinished:    called every time after the odata requestExecution finished  (no matter that it failed or succeeded).
  • requestCacheResponse:    called when the Store found entities for the current request in the cache. (not used in SP5 version, will be supported in SP6)

For this code snippet, SODataRequestDelegate is declared in the same class. (For the relatively complex app, which has many view controllers, you might want to declare it in another central data controller - but I think you get the idea.)

So the scheduleReadEntitySet: method will trigger those callbacks. Let's take a look at how we typically write the logic in the callback.

01  - (void) requestServerResponse:(id<SODataRequestExecution>)requestExecution
02  {
03      id<SODataRequestParam> requestParam = requestExecution.request;
04   
05      if ([requestParam conformsToProtocol:@protocol(SODataRequestParamSingle)]) {
06          id<SODataRequestParamSingle> request = (id<SODataRequestParamSingle>)requestParam;   
07          if (request.mode == SODataRequestModeRead) {
08              id<SODataResponseSingle> responseSingle = (id<SODataResponseSingle>)requestExecution.response;
09              if ([responseSingle.payload conformsToProtocol:@protocol(SODataEntitySet)]) {  
10                  id<SODataEntitySet> myEntityset = (id<SODataEntitySet>)responseSingle.payload;
11              } else if ([responseSingle.payload conformsToProtocol:@protocol(SODataEntity)]) {
12                  id<SODataEntity> myEntity = (id<SODataEntity>)responseSingle.payload;
13              }
14          }
15      }
16  }

#05 checks if the request is general OData request (potentially the values could be others like batch request) and #07 checks if it was Read (= HTTP GET). #09 and #11 examine if the OData is either the entityset or entity type. #10 and #12 are demonstrating how you obtain the returned payload value as the object.


And here's a typical implementation example for requestFailed: callback method.

01  -(void)requestFailed:(id<SODataRequestExecution>)requestExecution error:(NSError *)error
02  {
03      NSString *msg1 = [NSString stringWithFormat:@"Request failed: %@", error.description];
04      if ([requestExecution.response conformsToProtocol:@protocol(SODataResponseSingle)]) {
05          id<SODataResponseSingle> response = (id<SODataResponseSingle>)requestExecution.response;
06          if ([[response payload] conformsToProtocol:@protocol(SODataError)]) {
07              NSString *msg2 = [(id<SODataError>)[response payload] message];
08          }
09      }
10  }

#03 is the error description from the callback method. #06 checks the payload is SODataError and #07 obtains the error message string from the payload.


That's all to request and response the entityset. You went through lots of new important things 🙂 and the next blog will talk about how we work with returned entityset or entity - together with CUD methods.



See you in the next blog,

Ken


List of blogs

22 Comments