在iPhone应用程序中使用RestKit的最佳方法 [英] Best way to use RestKit in an iPhone Application

查看:93
本文介绍了在iPhone应用程序中使用RestKit的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个iPhone应用程序,我终于决定使用RestKit作为连接REST服务的框架。

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

我想构建的方法是让我的应用程序中的控制器与RestKit完全无关。例如。如果我有一个登录屏幕,在通常的RestKit场景中(基于示例程序以及RestKit开发人员创建的一些博客条目),您将让控制器实现RKRequestDelegate协议并使用RKClient在Controller传递中调用服务自我(控制者)作为代表。我想隐藏用户开发控制器和视图。

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.

我想到的是以下内容。我将有一个登录用户的LoginService。会有协议LoginServiceDelegate,它有两个成功和失败的方法。 Controller可以实现LoginServiceDelegate并在LoginService中调用login方法并获得成功或失败回调。但是要做到这一点,我需要一些方法让我的LoginService将调用委托给控制器。 RestKit不允许我这样做,这是我能够通过使用LoginServiceDelegate初始化LoginService,将该委托存储为属性并在成功登录或失败时在委托中调用适当的方法来实现此目的的唯一方法。

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.

这使我的Controller代码库保持最小,并完全隐藏了LoginService的工作方式以及它内部使用的框架。委托人的使用也使Controller与模型分离,因此我们有一个很好的MVC。但是我担心Model类保留Controller对象的含义,因为它保留在委托上。

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.

你会如何使用RestKit?如果你认为我的方法很好,你会改变什么来改善它?如果您不喜欢我的方法,那么您希望得到反馈,以了解为什么您认为这不是一个好习惯。

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 <RKRequestDelegate>{
    NSObject<LoginServiceDelegate> *_loginServiceDelegate;

}

@property (retain, nonatomic) NSObject <LoginServiceDelegate> *loginServiceDelegate;

- (id) initWithDelegate:(NSObject<LoginServiceDelegate>*) loginServiceDelegate;

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

@end

@protocol LoginServiceDelegate
@optional

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

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

@end

干杯!!!

推荐答案

我是RestKit的作者,我们主张使用这些模式在RestKit之上构建更高级别的抽象。我通常在模型对象周围构建我的回调,而不是创建一个新的LoginService类型的对象,但两种方式都可以。在我的示例中,您将执行以下操作:

I'm the author of RestKit and we advocate using such patterns to build higher level abstractions on top of RestKit. I generally build my callbacks and such around a model object rather than creating a new LoginService type of object, but either way is fine. In my example, you would do something like:

@implementation RKUser
- (void)loginWithDelegate:(NSObject<RKUserAuthenticationDelegate>*)delegate {}
@end

@protocol RKUserAuthenticationDelegate
- (void)userDidLogin:(RKUser*)user;
- (void)userDidFailLoginWithError:(RKUser*)user;
- (void)userDidLogout:(RKUser*)user
@end

在任何情况下,我建议的另一件事是将您的委托从保留更改为分配。在你的dealloc方法中,你可以做几件事:

In any case, the other thing I would recommend is changing your delegate from a retain to an assign. In your dealloc method, you can do a couple of things:


  1. 没有代理,所以你不会被回调崩溃

  2. 要求请求队列取消任何请求: [[RKRequestQueue sharedQueue] cancelRequestsWithDelegate:self];

  1. Nil out the delegate so you won't get crashed by a callback
  2. Ask the request queue to cancel any requests: [[RKRequestQueue sharedQueue] cancelRequestsWithDelegate:self];

从内存管理/内务管理的角度来看,这就是你需要担心的一切。我通常总是做的另一件事是为我的身份验证生命周期事件创建通知。根据我的经验,你总是需要观察它们来更新UI。

That's about all that you need to worry about from a memory management / house-keeping perspective. The other thing that I typically always wind up doing is creating notifications for my authentication life-cycle events. You just always wind up needing to observe them to update UI somewhere in my experience.

你走在正确的轨道上并且设计很好。

You are on the right track and the design is fine.

最好,
Blake

Best, Blake

这篇关于在iPhone应用程序中使用RestKit的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆