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:
And three optional callback methods:
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 |