2013-05-05 49 views
0

我在制作JSON查询时遇到了模拟MVC体系结构的问题。使用JSON查询模拟MVC体系结构

下面我通过创建一个单独的NSObject类来处理查询异步获取数据。

这就是说,我很难将我应该放在TableViewController下面的查询方法中。

TableViewController.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //refactoring with MVC 
    self.aQueue = [[[NSOperationQueue alloc] init] autorelease]; 
    self.storeLogos = [NSMutableDictionary dictionary]; 

    [self queryStoreData]; 

} 
    -(void)queryStoreData 
{ 
    aStoreQuery = [StoreQuery queryForStores:self]; 
} 


-(void)query:(StoreQuery *)query queryResult:(id)object 
     { 
      [self.aQueue addOperationWithBlock:^{ 

       //JSONKit? 




      } 

     } 

StoreQuery.m

@synthesize myConnection, myRequest, storeData; 

+(StoreQuery*)queryForStores:(id<StoreQueryDelegate>)aDelegate 
{ 
    StoreQuery *storeQuery = [[[StoreQuery alloc] init] autorelease]; 
    storeQuery.delegate = aDelegate; 
    storeQuery.myRequest = [NSURLRequest requestWithURL:@"URL"]; 
    storeQuery.myConnection = [NSURLConnection connectionWithRequest:storeQuery.myRequest delegate:storeQuery]; 
    storeQuery.storeData = [[NSMutableArray data] retain]; 
    return storeQuery; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [self.storeData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [self.storeData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection 
    didFailWithError:(NSError *)error 
{ 
    NSLog(@"Connection Error: %@",[error localizedDescription]); 
    if (self.delegate) { 
     [self.delegate request:self didFailWithError:error]; 
    } 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    if (self.delegate) { 
     [_delegate request:self didFinishWithObject:self.storeData]; 
    } 
} 


- (void)dealloc 
{ 
    [myRequest release]; 
    [myConnection release]; 
    [storeData release]; 
    [super dealloc]; 
} 

回答

1

我倾向于以消除整个StoryQuery类(你似乎没有真正需要什么,这里的NSURLConnectionDataDelegate方法),并只使用基于块的网络电话,使用标准NSURLConnection或精彩AFNetworking框架。

标准NSURLConnection技术:

-(void)queryStoreData 
{ 
    NSURL *url = [NSURL URLWithString:@"yoururl"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [NSURLConnection sendAsynchronousRequest:request 
             queue:self.aQueue 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

           if (error) 
           { 
            NSLog(@"%s: error retrieving data = %@", __FUNCTION__, error); 
            return; 
           } 

           // now parse the results, e.g., if it was JSON: 

           NSError *parseError = nil; 
           self.results = [NSJSONSerialization JSONObjectWithData:data 
                       options:0 
                       error:&parseError]; 
           if (parseError) 
           { 
            NSLog(@"%s: error parsing data = %@", __FUNCTION__, parseError); 
            return; 
           } 

           // note, all user interface updates must happen on main queue, e.g. 

           [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
            [self.tableView reloadData]; 
           }]; 
          }]; 
} 

或者使用AFNetworking

-(void)queryStoreData 
{ 
    NSURL *url = [NSURL URLWithString:@"yoururl"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^ 
             (NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     self.results = JSON; 
     [self.tableView reloadData]; 
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
     NSLog(@"JSON network request failed: %@", error); 
    }]; 
    [operation start]; 
} 
+0

顺便说一下,虽然我试图提供如何做到这一点的真实示例,但您的问题引用了“MVC体系结构”。建立网络请求和适当的模型 - 视图 - 控制器体系结构之间的连接很薄。但是如果你真的关心正确的MVC架构,你可能不会将结果数组作为控制器对象的成员,但实际上可能会使用一些模型对象。但我假设真正的问题是如何执行网络操作并消耗结果,而不是MVC体系结构。 – Rob 2013-05-05 20:33:55

0

为MVC最好的例子是建筑AFNetworking Example。 它已通过使用Blocks实施。

+0

答案应该不仅仅是一个链接越多,你应该给的链接涉及什么说明,所以答案会站在没有链接 – Mark 2013-05-14 16:20:44