5

JSONまたはXMLAPIを使用して、非同期のNSURLConnectionを使用してURLからプロジェクトに関するデータを取得し、それをNSMutableArrayに解析してから、 NSTableViewにデータを入力するとします

モデルがあります:プロジェクト コントローラーがあります:TableViewController(テーブルデータソースおよびデリゲートとして機能します)

リクエストを開始し、結果を解析するコードをNSMutableArrayのどこに配置すればよいですか。

私が持っている必要があります:

1:

Project内のメソッドが呼び出さ-(NSMutableArray* ) getAllProjectsれ、コントローラーからこれを呼び出します。

または2:

たとえば、コントローラー内でProjectsArray*と呼ばれるProject*オブジェクトのNSMutableArrayを列挙する必要があります。呼び出すたびに?[[Project alloc] init]

オプション1は、複数のコントローラーからすべてのプロジェクトを取得したい場合があり、繰り返しコードを節約できるため、私にははるかに理にかなっています。プロジェクトモデル内でパブリックメソッドを呼び出すだけで済みます。この場合、私はたくさんの[[self alloc] init]ステートメントを実行しますか?これでいい?また、私のモデルはNSURLConnectionデリゲートである必要があります。これは正しいです?

4

3 に答える 3

2

間違いなくあなたのモデルにあるはずです。

理由 :

さまざまなコントローラーから何度も更新する必要があるため、将来的には KVO を使用できます。

于 2012-11-06T14:31:22.940 に答える
1

From my experience, I think the good way is to have the parsing routines in the model (ProjectsArray) and connection stuff in another class, which initiates the connection and returns raw NSData (through a delegate for example), which you pass to the model to parse it. This way your model or viewController won't have multiple roles.

[[Project alloc] init]データが必要になるたびに呼び出すことについては、モデルクラスで静的参照を使用してから、次のような方法で取得できます- (ProjectsArray *)instance

于 2012-11-06T16:39:13.487 に答える
0
/*
 * 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
于 2013-03-01T04:14:34.020 に答える