0

This is the entire code of my Foundation test app: (shrunk to the minimum possible)

#import <Foundation/Foundation.h>
#import "AFNetworking.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:@"http://www.ansa.it" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"success");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"failure");
        }];

        // Trying both with -waitUntilAllOperationsAreFinished and with making the main thread sleep
        [manager.operationQueue waitUntilAllOperationsAreFinished];
        //[NSThread sleepForTimeInterval:5];
        NSLog(@"Die!");
    }
    return 0;
}

I can see that the request starts (LittleSnitch shows a connection attempt to ansa.it - a test domain), but no completion block is ever called.
Additionally, if I try to set a breakpoint on the completion block inside the AFHTTPRequestOperation class ( https://github.com/AFNetworking/AFNetworking/blob/bb39a33ba242b50a2d779e37b4a836f4471160a5/AFNetworking/AFHTTPRequestOperation.m#L115 line 115), I see it never gets called.
There is plenty of time before the process dies, and anyways I'm making sure that doesn't happen before the request is complete, by telling the main thread to wait for the queue to be done and/or to pause the main thread.

I'm developing on OSX 10.10 Yosemite GM 2.0 (last version until 3 hours ago), XCode 6.1 GM on x86_64.
I'm getting errors with both the last stable release of AFNetworking (2.4.1 as of writing) and the code on the master branch.

Please help!

ItalyPaleAle
  • 7,185
  • 6
  • 42
  • 69
  • 1
    [it need a run loop](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html#//apple_ref/occ/instm/NSRunLoop/runUntilDate:) – Bryan Chen Oct 10 '14 at 00:22

1 Answers1

2

Without a runloop, main will execute once and exit immediately.

See this Stack Overflow question for more information.

Community
  • 1
  • 1
mattt
  • 19,544
  • 7
  • 73
  • 84
  • But on the main thread I'm waiting for the queue to be done (which runs on another thread). Shouldn't that be enough? – ItalyPaleAle Oct 10 '14 at 01:15
  • Well, I mean, if it were, you wouldn't have this issue... You need a runloop for `main` to not exit immediately. – mattt Oct 10 '14 at 01:30
  • main() was not exiting immediately! – ItalyPaleAle Oct 10 '14 at 01:33
  • Anyways, the run loop solved it! I think I understand now: while main() was not exiting immediately, the main thread was paused and could not perform any message (for example dispatching the new threads, etc). This code in particular worked excellently: http://stackoverflow.com/a/26178578/192024 Thanks! – ItalyPaleAle Oct 10 '14 at 01:34