-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Closed
Description
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 commentedon Oct 8, 2014
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 commentedon Nov 1, 2014
So about this issue how to implementation modified configuration with manager instead of using default configuration with sharedInstance?
yaozhai commentedon Nov 18, 2014
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 commentedon Nov 20, 2014
@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 customNetworkManager
class:cloud-hot commentedon Nov 20, 2014
@rainypixels , Thanks! I will try it later.
hengchengfei commentedon Aug 26, 2015
http://stackoverflow.com/questions/30906607/about-alamofire-version-for-use-manager
guoyu1989 commentedon Sep 26, 2015
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 commentedon Oct 2, 2015
help me !!!!
Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=0x78fc41e0 {NSErrorFailingURLKey=myURL, NSErrorFailingURLStringKey=myURL, NSLocalizedDescription=cancelled}
guoyu1989 commentedon Oct 2, 2015
Can you print out the sample request using the debugprint()
DFernando28 commentedon Oct 2, 2015
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 commentedon Oct 2, 2015
Are you sure your url is correct, since the address (https://vodafoneplazaapp.vodafone.es:20000/api.php) shows no endpoint.
15 remaining items