我在进行 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];
}
最佳答案
我倾向于消除整个 StoryQuery
类(您似乎并不真正需要任何 NSURLConnectionDataDelegate
方法),而只使用一个 block -基于网络调用,使用标准的 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];
}
关于iphone - 使用 JSON 查询模拟 MVC 架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16387411/