iphone - 如何在我的 iOS 应用程序中正确实现服务类?

标签 iphone ios web-services architecture afnetworking

我目前的难题:实现一个模型类,专门用于对我的 Rails 应用程序进行服务调用。

场景如下:

  • 我有一个名为 Service 的类,它是 NSObject 的子类。
  • 实现文件定义了一些方法……让我们看看 doSignUp
  • 我正在使用 AFNetworking 与 API 进行通信。
  • 在我的 SignUpViewController 中,我创建了一个实例 服务类并调用doSignUp
  • 该方法按预期工作,并且我从服务器收到了正确的响应。

现在是我不完全理解的部分:

  • AFNetworking 利用 block 进行服务调用。
  • success block 中,我调用了一个名为 handleSignUp 的辅助方法(也在 Service 类中)。这个方法本质上是解析 JSON,我从中创建了一个新的 User(NSObject 子类)。 handSignUp 方法然后返回 User 对象。

此时我有一个新的 User 对象,我需要将该对象发送回我的 SignUpViewController...我该怎么做?

  • 我应该尝试将该对象添加到 AppDelegate 并访问它吗 来自SignUpViewController?该解决方案可以用于访问 各种全局属性,但是什么时候 SignUpViewController 知道什么时候访问它?
  • 我是否应该尝试添加对 SignUpViewController 的引用 服务类?这似乎适得其反......我可能会 将 doSignUphandSignUp 方法添加到 注册 View Controller 。在我看来,我的 Service 类不应该知道任何其他 viewController。

下面是我的代码示例:

服务.h

//Service.h
#import <UIKit/UIKit.h>
#import "AFNetworking.h"
@interface Services : NSObject
- (void) doSignUp:(NSMutableDictionary*)params;
@end

服务.m

// Service.m
 #import "Services.h"
 #import "Config.h"
 @implementation Services

 - (void) doSignUp:(NSMutableDictionary*)params {
     NSURL *url = [NSURL URLWithString:@"http://MYURL.COM"];
     AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
     NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"signup.json" parameters:params];
     AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
         [self handleSignUp:JSON];
     } failure:nil];
     [operation start];
 }

 - (User*) handleSignUp:(NSMutableArray*)json {
     User * user = nil;
     if ([[json valueForKey:@"success"] isEqualToString:@"true"]) {
           // create user here ...
     }
     return user;
 }

注册 View Controller .h

#import "Service.h"
 @interface SignUpViewController : UIViewController {
     Service * service;
 }

 @property (nonatomic, strong) Service * service;

SignUpViewController.m

#import "SignUpViewController.h"
 @interface SignUpViewController ()
 @end

 @implementation SignUpViewController

 @synthesize service = __service;
 - (IBAction) initSignUp:(id)sender {
      // create params...
      [self.service doSignUp:params];
 }

再说一次,所有这些代码都在做它应该做的事情……我只需要知道它们应该如何通信。我如何提醒 SignUpViewController handleSignUp 已被调用并且新的 User 对象可用?

谢谢你的时间, 安德烈

最佳答案

据我所知,您没有意识到这个过程的异步性质:实际上,由于网络或 IO 操作需要很长时间才能完成,我们倾向于使用异步访问,我的意思是:调用者( View Controller )调用一个通过该服务在 Web API 上发送消息,然后停止等待响应。实际上,网络处理可以在不同的进程或线程或处理器的核心上完成。

那么,当操作完成时,服务如何通知调用者呢? 为此,我们必须使用委托(delegate)绑定(bind)两个对象: 我们创建了一个协议(protocol)(它只是一个对象将响应的消息的声明),我们在我们的 View Controller 中实现它,这样无论谁将使用它,都会确定哪些消息可用。

然后我们将在我们的服务中声明一个类型为 id 的委托(delegate),一旦操作完成就会调用该委托(delegate)。

这几乎就像在服务类中导入您的 ViewController.h 并为服务提供指向 View Controller 的指针,但以正确的方式完成(没有循环引用并遵守 SOLID 原则)

现在是一些代码:

//service.h
@protocol RemoteApiProtocol <NSObject>
@required
-(void) UserLoggingIn;
-(void) UserLoggedIn:(User*)user;
-(void) UserLoginError:(NSError *)error;
@end

协议(protocol)就像一个小接口(interface)一样工作,它是两个类/对象之间的契约。 然后在您的服务中,您可以为协议(protocol)声明字段和属性:

//Service.h
#import <UIKit/UIKit.h>
#import "AFNetworking.h"
@interface Services : NSObject{
      __weak id<RemoteApiProtocol> delegate;
}

@property (nonatomic, assign) id<RemoteApiProtocol> delegate;

- (void) doSignUp:(NSMutableDictionary*)params;

@end

此时,您在 View Controller 中实现了协议(protocol),集成了您刚刚构建的合约。这样服务器就会知道代理将始终能够响应协议(protocol)消息。

//SignUpViewController.h
#import "Service.h"
 @interface SignUpViewController : UIViewController <RemoteApiProtocol> {
     Service * service;
 }

 @property (nonatomic, strong) Service * service;

在实现协议(protocol)后,您可以将您的 View Controller 指定为服务器的委托(delegate),这样服务器在调用 api 后将能够使用调用结果调用委托(delegate)。为此,您需要实现服务将调用的协议(protocol)消息:

//SignUpViewController.m
#import "SignUpViewController.h"

 @implementation SignUpViewController

 @synthesize service = __service;
 - (IBAction) initSignUp:(id)sender {
      //create the service with params
      //...

      //assign self as the delegate
      self.service.delegate = self;
      [self.service doSignUp:params];
 }

#pragma mark - RemoteApiProtocol
-(void) UserLoggingIn
{
      //login started, 
      //you can show a spinner or animation here
}
-(void) UserLoggedIn:(User*)user
{
      //user logged in , do your stuff here
}
-(void) UserLoginError:(NSError *)error
{
      NSLog(@"error: %@",error);
}

@end

在调用成功和错误 block 中的 api 之后,最后调用服务中的消息:

// Service.m
 #import "Services.h"
 #import "Config.h"
 @implementation Services

 - (void) doSignUp:(NSMutableDictionary*)params {
     NSURL *url = [NSURL URLWithString:@"http://MYURL.COM"];
     AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
     NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"signup.json" parameters:params];
     AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
         [self handleSignUp:JSON];
     } failure:nil];


     //Signaling the start of the signup operation to the delegate
     if(self.delegate){
          [self.delegate UserLoggingIn];
     }

     [operation start];
 }

 - (User*) handleSignUp:(NSMutableArray*)json {
     User * user = nil;
     if ([[json valueForKey:@"success"] isEqualToString:@"true"]) {
           // create user here ...
           //call the delegate (view controller) passing the user
           if(self.delegate){
               [self.delegate UserLoggedIn:user];
           }
     }else{
           //error handling
           if(self.delegate){
               //NSError *error = ...
               //[self.delegate UserLoginError:error];
           }
     }


     //return user;
 }
@end

关于iphone - 如何在我的 iOS 应用程序中正确实现服务类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11007892/

相关文章:

iphone - 适用于iPhone应用程序的iOS 3.0+兼容性

ios - 找不到 AFNetworking 文件

java - 通过Web服务从服务器向客户端发送pdf文件

c++ - GWT 到 C++ 通信的选项有哪些?

iphone - 如何从 UIMenuController 中删除复制、全选、定义菜单项

ios - 如何减少UIImageView上图像显示的大小

ios - iOS App 的后台状态、非事件状态和非运行状态有什么区别?

ios - 更改 Facebook 登录按钮的外观

iOS:UIImage 上的 EXC_BAD_ACCESS:drawInRect 有 2 个图像

java - 安装 JAX-RS(REST Web 服务)1.1 时失败。 java.lang.NullPointerException异常