Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do I leverage MVCS methodologies with a JSON query?

Right now I have a simple array query:

GetArray.h
+ (NSArray *)getArray
{
    NSString *rawJson = [NSString stringWithFormat:@"A_JSON_URL"];
    NSString *jQuery = [rawJson stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *jData = [NSData dataWithContentsOfURL:[NSURL URLWithString:jQuery]];
    NSString *jString = [[NSString alloc] initWithData:jData encoding:NSUTF8StringEncoding];
    NSDictionary *jDict = [jString JSONValue];
    NSArray *anArray = [jDict objectForKey:@"things"];
    [jString release];
    return anArray;
}

In my viewDidLoad in my MainTableViewController.m I have this:
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myData = [GetArray getArray];

}
I populate my table view here at cellForRowAtIndexPath:
NSString *widget.myData = [[self.myData objectAtIndex:indexPath.row] objectForKey:@"widget"];
    cell.widget.text = widget;
However I want to keep my logic separate from my model.

I need to make an asynchronous call to pull pictures of all the widgets. I could put a this in my cellForRowAtIndexPath:
 __block NSString *widgetID = [[self.myData objectAtIndex:indexPath.row] objectForKey:@"widgetNumber"];
    dispatch_queue_t downloadQueue = dispatch_queue_create("picture pull", NULL);
    dispatch_async(downloadQueue, ^{
        NSURL *logoURL = [NSURL URLWithString:imageURLString];
        NSData *logoData = [NSData dataWithContentsOfURL:imageURL];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *aWidgetID = [[self.myData objectAtIndex:indexPath.row] objectForKey:@"widgetNumber"];
            if ([aWidgetID isEqualToString:widgetID]) cell.pic.image =[UIImage imageWithData:picData];
        });
    });
    dispatch_release(downloadQueue);
But this seems like it would go against the grain as far as MVC goes.

So, how can I abstract all of my JSON logic?
Tagged:

Replies

  • DomeleDomele Posts: 2,991 @ @ @ @ @
    edited May 2013
    Personally, I'd create a Widget NSObject subclass. Then, I'd create properties for all the data you need (like a UIImage property for the picture).

    Then I'd create a fetcher class that works asynchronously and either has a delegate or completion handler to notify when it's done downloading all the JSON data and parsing it into Widget objects. This fetcher class would hand back an array of Widget objects.

    Then I'd give the Widget class a method like downloadPictureWithCompletionHandler:(WidgetPictureDownloadCompletionHandler)handler;
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.<br />
    <br />
    New app - See screenshots and details at <a href="http://www.globaclock.com"; target="_blank">www.globaclock.com</a>.<br />
    <br />
    If you want to
Sign In or Register to comment.