0

在进行 JSON 查询时,我无法模拟 MVC 架构。

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

这就是说,我很困惑我应该在 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];
}
4

2 回答 2

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];
}
于 2013-05-05T20:29:05.933 回答
0

MVC 架构的最佳示例是AFNetworking 示例。它是通过使用Blocks实现的。

于 2013-05-05T18:09:38.187 回答