Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:55:35+00:00 2026-06-13T05:55:35+00:00

I’ve got this parsing operation that currently works fine, but I’ve started to notice

  • 0

I’ve got this parsing operation that currently works fine, but I’ve started to notice that it is freezing up my UI slightly so I’m trying to refactor and get this done asynchronously. I’m having some issues however and was hoping someone could point me in the right direction. Here’s my current (synchronous) code:

- (NSArray *)eventsFromJSON:(NSString *)objectNotation
{
    NSParameterAssert(objectNotation != nil);
    NSData *unicodeNotation = [objectNotation dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *eventsData = [NSJSONSerialization JSONObjectWithData:unicodeNotation options:0 error:&error];

    if (eventsData == nil) {
            //invalid JSON
            return nil;
        }

        NSArray *events = [eventsData valueForKeyPath:@"resultsPage.results"];
        if (events == nil) {
            //parsing error
            return nil;
        }

        NSLog(@"events looks like %@", events);
        NSMutableArray *formattedEvents = [NSMutableArray arrayWithCapacity:events.count];
        for (id object in [events valueForKeyPath:@"event"]) {
            Event *event = [[Event alloc] init];
            event.latitude = [object valueForKeyPath:@"location.lat"];
            event.longitude = [object valueForKeyPath:@"location.lng"];
            event.title = [object valueForKeyPath:@"displayName"];
            event.venue = [object valueForKeyPath:@"venue.displayName"];
            event.ticketsLink = [NSURL URLWithString:[object valueForKeyPath:@"uri"]];
            event.artist = [object valueForKeyPath:@"performance.artist.displayName"];
            event.date = [object valueForKeyPath:@"start.datetime"];

            [formattedEvents addObject:event];
        }

    return [NSArray arrayWithArray:formattedEvents];

}

I’ve been looking into NSOperationQueue’s and I’m struggling to find a solution as I’d like to return an array from this method and operation queues are not meant to have return values. I’m also looking at GCD and i’ve got somethinbg like this:

- (NSArray *)eventsFromJSON:(NSString *)objectNotation
    {
dispatch_queue_t backgroundQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);


__block NSMutableArray *mutable = [NSMutableArray array];
dispatch_async(backgroundQueue, ^{
    NSParameterAssert(objectNotation != nil);
    NSData *unicodeNotation = [objectNotation dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *eventsData = [NSJSONSerialization JSONObjectWithData:unicodeNotation options:0 error:&error];

    if (eventsData == nil) {
        //invalid JSON
        mutable = nil;
    }

    NSArray *events = [eventsData valueForKeyPath:@"resultsPage.results"];
    if (events == nil) {
        //parsing error
        mutable = nil;
    }

    NSLog(@"events looks like %@", events);
    NSMutableArray *formattedEvents = [NSMutableArray arrayWithCapacity:events.count];
    for (id object in [events valueForKeyPath:@"event"]) {
        Event *event = [[Event alloc] init];
        event.latitude = [object valueForKeyPath:@"location.lat"];
        event.longitude = [object valueForKeyPath:@"location.lng"];
        event.title = [object valueForKeyPath:@"displayName"];
        event.venue = [object valueForKeyPath:@"venue.displayName"];
        event.ticketsLink = [NSURL URLWithString:[object valueForKeyPath:@"uri"]];
        event.artist = [object valueForKeyPath:@"performance.artist.displayName"];
        event.date = [object valueForKeyPath:@"start.datetime"];

        [formattedEvents addObject:event];
    }

    mutable = [NSMutableArray arrayWithArray:formattedEvents];

});

return [mutable copy];
}

For some reason, this seems to be returning the object before the parsing has finished however, as I’m gettting no data out of that mutable object, but I’m noticing that the parsing is indeed occurring (i’m logging out the results). can anyone give me an idea about how to get this asynch stuff going?

Thanks!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:55:36+00:00Added an answer on June 13, 2026 at 5:55 am

    You primary problem is that by their very nature asynchronous operations can’t synchronously return a result. Instead of returning an array from -eventsFromJSON:, you should provide a way for the caller to receive a callback when the results are finished. There are two common approaches to this in Cocoa.

    You can create a delegate with an associated delegate protocol including a method like -parser:(Parser *)parser didFinishParsingEvents:(NSArray *)events, then have your parser call this method on its delegate when parsing is finished.

    Another solution is to allow the caller to provide a completion block to be executed when parsing is complete. So, you might do something like this:

    - (void)eventsFromJSON:(NSString *)objectNotation completionHandler:(void (^)(NSArray *events))completionHandler)
    {
        dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
        dispatch_async(backgroundQueue, ^{
            NSMutableArray *mutable = [NSMutableArray array];
            NSParameterAssert(objectNotation != nil);
            NSData *unicodeNotation = [objectNotation dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error = nil;
    
            // Snip...
    
            mutable = [NSMutableArray arrayWithArray:formattedEvents];
    
            dispatch_async(dispatch_get_main_queue(), ^{
                completionHandler([mutable copy]);
            });
        });
    }
    

    Then you can call this code some thing like this:

     - (void)parseJSONAndUpdateUI // Or whatever you're doing
    {
        NSString *jsonString = ...;
        Parser *parser = [[Parser alloc] init];
        [parser parseEventsFromJSON:jsonString completionHandler:^(NSArray *events){
            // Update UI with parsed events here
        }];
    }
    

    I like the second, block-based approach better. It makes for less code in most cases. The code also reads closer to the synchronous approach where the method just returns an array, since the code that uses the resultant array simply follows the method call (albeit indented since it’s in the completion block’s scope).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.