Skip to content

Error Domain=NSURLErrorDomain Code=-999 "cancelled" #157

@mh8a

Description

@mh8a

I want to create a custom manager instance and use it:

var configuration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()

//add the Alamofire default headers to the configuration
configuration.HTTPAdditionalHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders

let manager = Alamofire.Manager(configuration: configuration)

let url = NSURL(string: "http://192.168.0.10/test")

manager.request( NSURLRequest(URL: url) )
    .response{(request, response, _, error) in
        println("\(error)")
    }

Which gives me an error:
Error Domain=NSURLErrorDomain Code=-999 "cancelled"

If i try this with the Singleton it works fine:

//let manager = Alamofire.Manager(configuration: configuration)
let manager = Alamofire.Manager.sharedInstance

Shouldn't the above code work with a custom instanced manager, too?

Thanks in advance.

Activity

mattt

mattt commented on Oct 8, 2014

@mattt
SponsorContributor

The difference here is that the initialized manager is not owned, and is deallocated shortly after it goes out of scope. As a result, any pending tasks are cancelled.

cloud-hot

cloud-hot commented on Nov 1, 2014

@cloud-hot

So about this issue how to implementation modified configuration with manager instead of using default configuration with sharedInstance?

yaozhai

yaozhai commented on Nov 18, 2014

@yaozhai

I had the same problem, is there a way to set up a specific session manager for modification instead of modifying the global default one?

rainypixels

rainypixels commented on Nov 20, 2014

@rainypixels

@cloud-hot @kohakugawa You just have to ensure that manager is retained. There are lots of ways you can do this. At the simplest level, for example, you could make it a stored property for a custom NetworkManager class:

import Foundation
import Alamofire

class NetworkManager {

    var manager: Manager?

    init() {
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        manager = Alamofire.Manager(configuration: configuration)
    }
}
cloud-hot

cloud-hot commented on Nov 20, 2014

@cloud-hot

@rainypixels , Thanks! I will try it later.

hengchengfei

hengchengfei commented on Aug 26, 2015

@hengchengfei
guoyu1989

guoyu1989 commented on Sep 26, 2015

@guoyu1989

After trying all the solutions provided above, my code still fails with same error. For my case, it's because I made too many requests within same session, which exceed the maximum limit (https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/#//apple_ref/occ/instp/NSURLSessionConfiguration/HTTPMaximumConnectionsPerHost).

Sample code:

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPMaximumConnectionsPerHost = 10 // Some arbitrary number that I feel big enough.
let manager = Alamofire.Manager(configuration: configuration)

DFernando28

DFernando28 commented on Oct 2, 2015

@DFernando28

help me !!!!

Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x78fc41e0 {NSErrorFailingURLKey=myURL, NSErrorFailingURLStringKey=myURL, NSLocalizedDescription=cancelled}

guoyu1989

guoyu1989 commented on Oct 2, 2015

@guoyu1989

Can you print out the sample request using the debugprint()

DFernando28

DFernando28 commented on Oct 2, 2015

@DFernando28

my method is:

-(NSURLSessionDataTask *)logingUser:(NSString *)user password:(NSString *)password completion:(void (^)(NSDictionary *results, NSError *error))completion {
NSURLSessionDataTask *task = [self POST:kBASE_URL
parameters:@{@"request" : @"login"}
constructingBodyWithBlock:^(id formData) {
[formData appendPartWithFormData:[user dataUsingEncoding:NSUTF8StringEncoding] name:@"user"];
[formData appendPartWithFormData:[password dataUsingEncoding:NSUTF8StringEncoding] name:@"password"];
}
success:^(NSURLSessionDataTask *task, id responseObject) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
if (httpResponse.statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(responseObject, nil);
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Received HTTP %ld", (long)httpResponse.statusCode);
completion(nil, nil);
});
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"ERROR: %@", [Utility localize:@"Connection_Error"]);
NSLog(@"ERROR-LOG: %@",error);
completion(nil, error);
});
}];
return task;
}

my Log show:

ERROR: Could not connect to the server

Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x78fc41e0 {NSErrorFailingURLKey=myURL, NSErrorFailingURLStringKey=myURL, NSLocalizedDescription=cancelled}

guoyu1989

guoyu1989 commented on Oct 2, 2015

@guoyu1989

Are you sure your url is correct, since the address (https://vodafoneplazaapp.vodafone.es:20000/api.php) shows no endpoint.

15 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mattt@nicopuri@dirablue@rainypixels@guoyu1989

        Issue actions

          Error Domain=NSURLErrorDomain Code=-999 "cancelled" · Issue #157 · Alamofire/Alamofire