Reputation: 3
I'm having trouble emulating MVC architecture when making a JSON query.
Below I'm fetching the data asynchronously by creating a separate NSObject class for handling the query.
That said I'm stumped as to what I should be putting in my query method below in 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];
}
Upvotes: 0
Views: 183
Reputation: 3999
The Best Example for MVC architectural is AFNetworking Example. It is Been Implemented by using Blocks.
Upvotes: 0
Reputation: 438232
I'd be inclined to eliminate the whole StoryQuery
class (you don't appear to really need any of the NSURLConnectionDataDelegate
methods here) and just use a block-based network call, using either standard NSURLConnection
or the wonderful AFNetworking
framework.
The standard NSURLConnection
technique:
-(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];
}];
}];
}
Or using 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];
}
Upvotes: 1