1

Ich verwende NSMutableURLRequest ist richtig arbeiten, wenn ichNSMutableURLRequest Senden 2 mal während NSURLSession mit

- (void)postAsynchronousOnQueue:(NSOperationQueue*)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))completionHandler { 
    NSURLRequest* request = [self createPostRequest]; 
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:completionHandler]; 
} 
-(NSURLRequest*)createPostRequest { 
     NSMutableURLRequest* request = [[HttpRequest requestWithRelativePath:@"/photo"] toNSMutableURLRequest]; 
      [request setHTTPMethod:@"POST"]; 
      [request setHTTPBody:[self encodeParamsForUpload]]; 
      return request; 
} 

verwenden Aber das Problem ist, wenn mein app im Hintergrundmodus ist, wird es nicht funktionieren.

Hier ist meine HttpRequest Klassenmethode:

+(id)requestWithRelativePath:(NSString*)docpath { 
    return [[HttpRequest alloc] initWithRelativePath:docpath server:server username:email password:password]; 
} 
-(id)initWithRelativePath:(NSString*)docpath server:(NSString*)server username:(NSString*)username password:(NSString*)password { 
    if (self = [super init]) { 
     docpath = [docpath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
     _request = [self createRequestWithDocPath:docpath server:server username:username password:password]; 
    } 
    return self; 
} 

- (NSMutableURLRequest*)createRequestWithDocPath:(NSString*)docpath server:(NSString*)server username:(NSString*)username password:(NSString*)password { 
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@%@", server, docpath]]; 
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]; 
    [request setTimeoutInterval:120.0]; 
    if ((username != nil) && (password != nil)){ 
     NSString *authStr = [NSString stringWithFormat:@"%@:%@", username, password]; 
     NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding]; 
     NSString *authValue = [NSString stringWithFormat:@"Basic %@", [self base64Encoding:authData]]; 
     [request setValue:authValue forHTTPHeaderField:@"Authorization"]; 
    } 
    return request; 
} 

Von, Stapelüberlauf fand ich NSURLSession API-Aufrufe im Hintergrund zu arbeiten. Also habe ich NSURLSession verwendet. Hier ist meine aktualisierte Code, die ich getan habe:

- (void)postAsynchronousOnQueue:(NSOperationQueue*)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))completionHandler { 
    NSURLRequest* request = [self createPostRequest]; 
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:completionHandler]; 
} 
-(NSURLRequest*)createPostRequest { 
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration]; 
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]]; 

    NSMutableURLRequest* request = [[HttpRequest requestWithRelativePath:@"/photo"] toNSMutableURLRequest]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:[self encodeParamsForUpload]]; 
    //Create task 
    NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     //Handle your response here 
     [[NSURLSession sharedSession]invalidateAndCancel]; 
    }]; 

    [dataTask resume]; 
    return request; 
} 

Aber, wenn ich NSURLSession bin mit der Anforderung sendet zweimal Ich habe bereits die Haltepunkte in NSMutableURLRequest Linie, sondern es nur einmal anrufen.

Bitte helfen Sie mir, das Problem zu lösen.

+0

Unrelated, aber ich möchte vermeiden, für jede Anfrage einen neuen 'NSURLSession' zu schaffen. Ich würde es einmal erstellen und es für jede Anfrage wiederverwenden. Oder benutze 'sharedSession'. Ich würde auch die Entwertung der 'NSURLSession' entfernen, unter der Annahme, dass Sie sie später noch einmal verwenden könnten. – Rob

Antwort

0

Ihre createPostRequest erstellt eine Anfrage und reicht sie über NSURLSession ein. Aber es gibt auch die NSURLRequest und postAsynchronousOnQueue Erlöse, um es erneut zu senden, diesmal durch die veraltete NSURLConnection.

Entfernen Sie den Code NSURLConnection und verlassen Sie sich nur auf NSURLSession, um die Anfrage zu stellen.


Zum Beispiel:

- (void)postAsynchronousOnQueue:(NSOperationQueue*)queue completionHandler:(void (^)(NSData *, NSURLResponse*, NSError*))completionHandler { 
    NSURLSession *defaultSession = [NSURLSession sharedSession]; 

    NSMutableURLRequest* request = [[HttpRequest requestWithRelativePath:@"/photo"] toNSMutableURLRequest]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:[self encodeParamsForUpload]]; 

    NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     //Handle your response here 
     [queue addOperationWithBlock:^{ 
      completionHandler(data, response, error); 
     }]; 
    }]; 

    [dataTask resume]; 
}