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 8046421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:46:50+00:00 2026-06-05T05:46:50+00:00

.h @property (strong) NSString *reply; I have the following method: .m @synthesize reply; –

  • 0
.h
@property (strong) NSString *reply;

I have the following method:

.m
@synthesize reply;

- (void)send
 {
 [NSURLConnection sendAsynchronousRequest:[self request] 
                 queue:[[NSOperationQueue alloc] init] 
                 completionHandler:
                        ^(NSURLResponse *response, NSData *data, NSError *error)       
                        {
                            if (error)
                            {
                                //NSLog(@"Error,%@", [error localizedDescription]);
                                [self setReply: [[NSString alloc] initWithString:[error localizedDescription]]];
                            }
                            else 
                            {
                                //NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
                                [self setReply: [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]];
                            } 
                        }];

}

Now, I have tried to return a value from the block/handler, but apparently this is not possible ( or I haven’t figured out the syntax yet ).

I have tried to set a local variable to get the value of the reply (Data) but it produces errors.

The only way that errors were not produced is by using a class property. But when I try to read the value of [self reply] it is null, which makes me think that it is never set.

I understand that the sendAsync function is threaded and asynchronous, so is that the reason that the value of [self reply] is null, while when I use a sendSynchronousRequest, I always get a reply ???

What am I doing wrong, and how can I return a value from within the completionHandler ???

Edit#1: It appears to me that Im doing something very wrong. I was tracing into the code, and when I used the synchronous send, everything worked fine. Using the asynchronous one, the actual call to sendAsynchronousRequest was never executed, making me think that the thread is not being invoked, and hence the empty value.

Edit#2: I have found a way around this, by adding the following:

[NSURLConnection sendAsynchronousRequest:[self request] 
                 queue:[NSOperationQueue alloc] init
                 completionHandler:
                        ^(NSURLResponse *response, NSData *data, NSError *error)       
                        {
                            if (error)
                            {
                                //NSLog(@"Error,%@", [error localizedDescription]);
                                [self setReply: [[NSString alloc] initWithString:[error localizedDescription]]];
                            }
                            else 
                            {
                                //NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
                                [self setReply: [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]];
                            } 
                        }];

[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 2]];

However, I am not entirely sure I understand how it works. I imagine that RunLoop tells the async operation to run for 2 seconds ( I can verify because when setting the date interval to a low value, I get a null response, due to testing with a live server ). What I do not understand, is when I just tell the RunLoop to run, it keeps blocking everything forever, as if the current thread never terminated.

  • 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-05T05:46:52+00:00Added an answer on June 5, 2026 at 5:46 am

    Almost a month later, I found the solution to my problem:

        NSURLConnection* _connection = [[NSURLConnection alloc] initWithRequest:[self request] delegate:self startImmediately:NO];
        self.port = [NSPort port];                                                                                                       
        self.runloop = [NSRunLoop currentRunLoop];                                                                                       
        [self.runloop addPort:self.port forMode:NSDefaultRunLoopMode];
        [_connection scheduleInRunLoop:self.runloop forMode:NSDefaultRunLoopMode];
        [_connection start];
        while (self.finished != YES ) {
            [self.runloop runUntilDate:[NSDate dateWithTimeIntervalSinceNow: 0.1]];                                                                    
        }
        [self.runloop removePort:[self port] forMode:NSDefaultRunLoopMode];
        [_connection unscheduleFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    

    This did the trick for me. Bear in mind I had to properly implement NSConnection’s delegate methods (didReceiveResponse,didReceiveData,didFailWithError,connectionDidFinishLoading).
    However it is now working asynchronously, does not freeze the UI, and has proper error reporting.
    Hope it may help someone stuck at the same issue

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

Sidebar

Related Questions

I have a class with the following property and method: header file below -
I have a custom UITableViewController with 2 properties: @property (strong, nonatomic) IBOutlet VenueHeaderViewController *header;
I have a class Events @interface MeEvents : NSObject { NSString* name;**strong text** NSString*
What I am doing is //ClassB.h @property (strong, nonatomic) NSString *name; and //ClassA.h @interface
In my interface (.h) file, I have @property(readonly) NSString *foo; and in my implementation
If you have a property in your public interface like the following @interface MyClass
MyObject : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSArray *notificationsArray; I
I have the following UIViewController class: FILE: ResultsDataViewController.h @interface ResultsDataViewController : UIViewController @property (nonatomic,
Take the following property: public string Foo { get; private set; } Using reflection,
I have a string property that I would like to be able to force

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.