Best way to use RestKit in an iPhone Application

I am writing an iPhone application and I have finally decided to use RestKit as the framework for connecting to REST Services.

The way I am thinking of building is to have the Controllers in my application be completely agnostic to RestKit. For eg. If I had a login screen, in the usual RestKit scenario (based on example programs as well as few blog entries created by the RestKit developers) you will have the controller implement the RKRequestDelegate protocol and use the RKClient to call the service in the Controller passing self ( the controller) as the delegate. I would like to hide that from the User developing the Controllers and views.

What I am thinking of is the following. I will have a LoginService which will login the user. There will be protocol LoginServiceDelegate which has two methods for success and failure. And the Controller can implement the LoginServiceDelegate and call the login Method in LoginService and get a success or failure callback. However to do this, I will need some way for my LoginService to delegate the calls back to the controller. RestKit does not allow me to do this and the only way I am able to do this by initializing the LoginService with a LoginServiceDelegate, storing that delegate as a property and calling the appropriate method in the delegate on successful login or failure.

This keeps my Controller codebase to a minimum and hides completely how the LoginService works and what framework it internally uses. The use of delegate also de-couples the Controller from the Models and so we have a good MVC thing going. However I am concerned about the implications of the Model class retaining the Controller object since it is holding onto the delegate.

How would you use RestKit ? If you think my approach is good , what would you change to make it better ? If you dont like my approach would like your feedback as to why you think it is not a good practice.

This below code snippet should give you a better idea

@protocol LoginServiceDelegate;

@interface LoginService : NSObject {
    NSObject *_loginServiceDelegate;

}

@property (retain, nonatomic) NSObject  *loginServiceDelegate;

- (id) initWithDelegate:(NSObject*) loginServiceDelegate;

- (void) login:(NSString *)username withPassword:(NSString *)password;

@end

@protocol LoginServiceDelegate
@optional

- (void) loginSuccess:(LoginInfo *) loginInfo;

- (void) loginFailure:(NSString *) message;

@end

Cheers !!!

49
задан Community 23 May 2017 в 02:14
поделиться