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

  • Home
  • SEARCH
  • 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 8192123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:06:07+00:00 2026-06-07T04:06:07+00:00

I’m using AFNetworking with AFHTTPRequestOperation to pull XML data from a webservice. This is

  • 0

I’m using AFNetworking with AFHTTPRequestOperation to pull XML data from a webservice. This is working fine and im getting the data I need but I need to split this data into objects and initialize a NSMutableArray with this data. This is working in the completion block, but just before I return the array in my method the data is gone? How do I do this?

Here is some of my code:

NSMutableArray *result = [[NSMutableArray alloc] init];    
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString* response = [operation responseString];
    NSData* xmlData = [response dataUsingEncoding:NSUTF8StringEncoding];
    NSError *xmlError;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&xmlError];
    NSArray *allElements = [doc.rootElement elementsForName:@"Misc"];
    for (GDataXMLElement *current in allElements)
    {
        NSString *titel;
        NSString *tekst;

        NSArray *titels = [current elementsForName:@"Titel"];
        if(titels.count > 0)
        {
            GDataXMLElement *firstTitel = (GDataXMLElement *) [titels objectAtIndex:0];
            titel = firstTitel.stringValue;
        } else continue;

        NSArray *teksts = [current elementsForName:@"Tekst"];
        if(teksts.count > 0)
        {
            GDataXMLElement *firstTekst = (GDataXMLElement *) [teksts objectAtIndex:0];
            tekst = firstTekst.stringValue;
        } else continue;

        HVMGUniversalItem *item = [[HVMGUniversalItem alloc] initWithTitel:titel AndTekst:tekst];
        [result addObject:item];
    }
    NSLog(@"%i", result.count);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", [operation error]);
}];

[operation start];
NSLog(@"%i", result.count);
return result;

What am I doing wrong? Why isn’t the data present in the array when returning?

  • 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-07T04:06:09+00:00Added an answer on June 7, 2026 at 4:06 am

    Why isn’t the data present in the array when returning?

    Because AFNetworking use an async pattern. So the return code will be performed before the operation will be completed.

    You need to use a different approach or follow Can AFNetworking return data synchronously (inside a block)?. The latter is discouraged.

    A solution could be to:

    -> Create a NSOperationQueue within your class that will include your operation. Create it as a property for your class like.

    @property (nonatomic, strong, readonly) NSOperationQueue* downloadQueue;
    
    - (NSOperationQueue*)downloadQueue
    {
        if(downloadQueue) return downloadQueue;
    
        downloadQueue = // alloc init here
    }
    

    -> Create a property for your array (synthesize also it)

    @property (nonatomic, strong) NSMutableArray* result;
    

    -> Wrap your code within a specific method like doOperation.

    self.result = [[NSMutableArray alloc] init];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    
    __weak YourClass* selfBlock = self; 
    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString* response = [operation responseString];
        NSData* xmlData = [response dataUsingEncoding:NSUTF8StringEncoding];
        NSError *xmlError;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&xmlError];
        NSArray *allElements = [doc.rootElement elementsForName:@"Misc"];
        for (GDataXMLElement *current in allElements)
        {
            NSString *titel;
            NSString *tekst;
    
            NSArray *titels = [current elementsForName:@"Titel"];
            if(titels.count > 0)
            {
                GDataXMLElement *firstTitel = (GDataXMLElement *) [titels objectAtIndex:0];
                titel = firstTitel.stringValue;
            } else continue;
    
            NSArray *teksts = [current elementsForName:@"Tekst"];
            if(teksts.count > 0)
            {
                GDataXMLElement *firstTekst = (GDataXMLElement *) [teksts objectAtIndex:0];
                tekst = firstTekst.stringValue;
            } else continue;
    
            HVMGUniversalItem *item = [[HVMGUniversalItem alloc] initWithTitel:titel AndTekst:tekst];
            [selfBlock.result addObject:item];
        }
        NSLog(@"%i", result.count);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", [operation error]);
    }];
    
    [downloadQueue addOperation:operation];
    

    -> if you need to notify that result has object send a notification, use the delegate pattern, etc…

    Hope that helps.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.