Hi
Right, if you added some custom methods to your DataAbstract service, then in order to call them, you will need to create a proxy object and call these methods from there.
DARemoteDataAdapter simplifies working with the base DataAbstractService and with login services.
I don’t see how use beginGetData?
We are supporting two approaches:
- Using delegates:
We need to create asynchronous DAAsyncRequest for the particular method. In case when we need to control exact moment of starting such asynchronous request we can specify NO as the start parameter, this will prevent request from being started automatically. Usually we may want to delay execution method until we configure request properly. We may need to specify the delegate instance or/and provide some context for the request. As the delegate we can use any instance of the class which implements DAAsyncRequestDelegate protocol. This protocol exposes set of the methods that could be called at successful completing asynchronous request of at it failure. All of the methods of that protocol are optional so you are free to implement only those you are really need here.
Later, after we configured our request, we can run it using start method which will run our request in the separate thread.
- (IBAction)loadData:(id)sender {
NSArray *fields = [NSArray arrayWithObjects:
@"WorkerID",
@"WorkerLastName",
@"WorkerFirstName",
@"WorkerPosition", nil];
// compose asynchronous request but not start it until we configure it properly
DAAsyncRequest* request = [remoteDataAdapter beginGetDataTable:@"Workers"
select:fields
where:nil
start:NO];
// setting self as the delegate means that the current class should be able to process
// the result of the async call
[request setDelegate:self];
// we can also specify any data to pass it to the result processing methods as the request context.
[request setContext:@"beginGetDataTable request"];
// run the request
[request start];
}
When the async method will ends its execution with success, then it will try to find asyncRequest:didReceiveTable: delegate method and invoke it with appropriate parameters. All we need here is to consume received data. Something like following:
- (void)asyncRequest:(DAAsyncRequest *)request
didReceiveTable:(DADataTable *)table {
// here we can obtain request context, we specified before, and use it for own purposes
id context = [request context];
NSLog(@"Context is: %@", id);
// consumes the data
[self setWorkersTable:table];
[tableView reloadData];
}
When the async method will ends with failure, then it will try to find and invoke asyncRequest:didFailWithException: delegate method. Here we should add our logic for proper handing various exceptions.
- (void) asyncRequest:(ROAsyncRequest *)request
didFailWithException:(NSException *)exception {
// handle the exception in right way
[self showException:exception];
}
- Using blocks:
The similar as above, but it doesn’t require setting delegate since all the code can be specified here in place as the blocks.
- (IBAction)loadData:(id)sender {
NSArray *fields = [NSArray arrayWithObjects:
@"WorkerID",
@"WorkerLastName",
@"WorkerFirstName",
@"WorkerPosition", nil];
// compose and run the asynchronous request
[remoteDataAdapter beginGetDataTable:@"Workers"
select:fields
where:nil
withBlock:^(DADataTable *table) {
// this code will be executed at the main thread when request ends
[self setWorkersTable:table];
[tableView reloadData];
}];
}
if we need to handle possible exceptions then it will be something like following:
- (IBAction)loadData:(id)sender {
NSArray *fields = [NSArray arrayWithObjects:
@"WorkerID",
@"WorkerLastName",
@"WorkerFirstName",
@"WorkerPosition", nil];
// compose asynchronous request but not start it until we configure it properly
DAAsyncRequest* request = [remoteDataAdapter beginGetDataTable:@"Workers"
select:fields
where:nil
start:NO];
[request setFailureBlock:^(NSException *exception){
// handle the exception in right way
NSLog(@"%@", exception);
}];
// run the request
[request startWithBlock:^{
// this code will be executed at the main thread when request ends
[self setWorkersTable:table];
[tableView reloadData];
}];
}
Hope this helps