【问题标题】:Do NSURLConnections belong in my model or controller?NSURLConnections 是否属于我的模型或控制器?
【发布时间】:2012-10-26 07:53:41
【问题描述】:

假设我正在使用 JSONXML API 从使用异步 NSURLConnection 的 URL 获取有关我的项目的数据,并将其解析为 NSMutableArray,然后填充 NSTableView

我有一个模型:项目 我有一个控制器:TableViewController(充当表数据源和委托)

我应该将启动请求并将结果解析为 NSMutableArray 的代码放在哪里。

我应该有:

1:

Project 中的一个名为 -(NSMutableArray* ) getAllProjects 的方法并从我的 Controller 中调用。

或者2:

我是否应该枚举 Project* 对象的 NSMutableArray,例如在我的 Controller 中调用 ProjectsArray*;每次调用[[Project alloc] init]

选项 1 对我来说更有意义,因为我可能想从多个控制器中获取所有项目,这样可以节省重复代码,我只需要在我的项目模型中调用一个公共方法。在这种情况下,我会做很多[[self alloc] init] 语句吗?这个可以吗?我的模型也需要是一个 NSURLConnection 委托。这是正确的吗?

【问题讨论】:

  • 我想你已经回答了你自己的问题。 :-) 隐藏数据来源或其准备是模型的有用行为。当然,“模型”不必只有一个类,根据您的描述,ProjectManager 对象可能是个好主意。

标签: objective-c ios xcode cocoa-touch cocoa


【解决方案1】:

毫无疑问,它一定在你的模型中。

原因:

因为您将需要从不同的控制器多次更新它,您可以在未来使用 KVO。

【讨论】:

  • 我不同意。模型类旨在封装数据并对其执行操作。像网络访问和编组这样复杂且耗时的操作属于控制器或实用程序类之外。
  • @psoft:这取决于我们的设计 :) 在设计 MVC 之前,人们已经在做类似的事情,所以这取决于我们。
【解决方案2】:

根据我的经验,我认为最好的方法是在模型 (ProjectsArray) 中使用解析例程,在另一个类中使用连接内容,它会启动连接并返回原始 NSData(例如通过委托),您可以通过到模型来解析它。这样您的模型或视图控制器将不会有多个角色。

至于每次需要数据时调用[[Project alloc] init]——你可以在模型类中使用静态引用,然后通过- (ProjectsArray *)instance之类的方式获取它

【讨论】:

    【解决方案3】:
    /*
     * UITableViewController has the word "controller" in it but that
     * does not make it a controller... it's a view. Pure and simple.
     *
     * /This/ is a controller...
     */
    
    @implementation MyController 
    
    @synthesize data; // assume readonly @property in interface
    
    -(void)fetchData {
    
       NSURLConnection *connection;
    
       // Set up URL connection, etc. Speaking very loosely, and
       // lossing over some important threading details...
    
       NSURLResponse *response = GetResponse();
    
       NSError *__autoreleasing error;
    
       @autoreleasepool {
          // build objects. premature optimization is the root of all evil,
          // but if you're really worried about too much allocation, you
          // can resist duplication with custom logic in parse().
          self.data = parse([response data], &error);
       }
    
       if (data == nil) {
         // Error notification
       }
    
       else { // Success notification
          NSNotification *didFetch = [NSNotification notificationWithName:@"didFetch" object:self.data userInfo:nil];
          [[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:didFetch waitUntilDone:NO];
       }
    }
    
    @end
    
    
    
    
    @interface MyTableViewController ()
       @property (unsafe_unretained) MyController *controller;
       @property (strong, nonatomic) NSArray *dataView;
    @end
    
    @implementation MyTableViewController
    
    @synthesize controller = _controller;
    @synthesize dataView = _dataView;
    
    -(void)viewDidLoad {
       _controller = [MyController controller]; // singleton
       [[NSNotificationCenter defaultCenter] addObserver:self
                                                selector:@selector(updateData:)
                                                    name:@"didFetch"
                                                  object:nil];
    }
    
    
    -(IBAction)buttonPress {
       [_controller fetchData]; // again, I'm glossing over threading details...
    }
    
    -(void)updateData {
       // Controller owns the data, we just get a view of it here.
       self.dataView = [[_controller data] arrayOfSomeSort];
       [self.view reloadData];
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-03
      • 2012-02-08
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2011-10-07
      • 1970-01-01
      相关资源
      最近更新 更多