new blog post, albert rest kit

This commit is contained in:
Albert 2013-09-03 14:08:19 +02:00 committed by Jörg Thalheim
parent 71416216f9
commit 3018bc637c
1 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,101 @@
---
layout: post
title: "Global response management with RestKit"
date: 2013-09-03 13:15
comments: true
categories: ios restkit
description: "RESTKIT how to handle request failures global"
---
For our latest iOS app we are using [RestKit Framework](http://restkit.org), which is a really great and advanced library to communicate to your REST API.
When you have lots of requests in different areas of your project, you may want to have a global handling for failure events.
For example how an Login View, if any of the requests gives you an 401 (Unauthorized) status code.
In RestKit 0.20 they introduced the oppertunity to register your own `RKObjectRequestOperation`, which is the common way to do this.
So at first you create a subclass of `RKObjectRequestOperation`, let's call it `CustomRKObjectRequestOperation`
```objective-c
#import "RKObjectRequestOperation.h"
@interface CustomRKObjectRequestOperation : RKObjectRequestOperation
@end
@implementation CustomRKObjectRequestOperation
- (void)setCompletionBlockWithSuccess:(void ( ^ ) ( RKObjectRequestOperation *operation , RKMappingResult *mappingResult ))success failure:(void ( ^ ) ( RKObjectRequestOperation *operation , NSError *error ))failure
{
[super setCompletionBlockWithSuccess:^void(RKObjectRequestOperation *operation , RKMappingResult *mappingResult) {
if (success) {
success(operation, mappingResult);
}
}failure:^void(RKObjectRequestOperation *operation , NSError *error) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"connectionFailure" object:operation];
if (failure) {
failure(operation, error);
}
}];
}
@end
```
This is the point where we overwrite the method which sets the completion and failure block.
I use the Observer Pattern (`NSNotificationCenter`) to notify about connectionFailures. ([Learn more about NSNotificationCenter](http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_nsnotificationcenter/))
Of course we need to tell RestKit to use our custom `RKObjectRequestOperation` class. You can do this by adding this line to you RestKit configuration:
```objective-c
[[RKObjectManager sharedManager] registerRequestOperationClass:[CustomRKObjectRequestOperation class]];
```
Now we need a class where we listen to the failure notifications. You can choose any of your class, I use the AppDelegate for this.
```objective-c
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionFailedWithOperation:) name:@"connectionFailure" object:nil];
```
As you should know, the `connectionFailedWithOperation:` is called when a connection failure occures.
```objective-c
- (void)connectionFailedWithOperation:(NSNotification *)notification
{
RKObjectRequestOperation *operation = (RKObjectRequestOperation *)notification.object;
if (operation) {
NSInteger statusCode = operation.HTTPRequestOperation.response.statusCode;
switch (statusCode) {
case 0: // No internet connection
{
}
break;
case 401: // not authenticated
{
}
break;
default:
{
}
break;
}
}
}
```
Links:
[RestKit Framework](http://restkit.org)
[Class Documentation for RKObjectRequestOperation](http://restkit.org/api/latest/Classes/RKObjectRequestOperation.html)
_by Albert Schulz_
If you have any questions feel free to contact me:
eMail: mail@halfco.de
Twitter: [@albert_sn](https://twitter.com/albert_sn)
Web: [halfco.de](http://halfco.de)