简体   繁体   English

dataTaskWithRequest 的 Objective-C 问题

[英]Objective-C issues with dataTaskWithRequest

I have recently switched from sendSynchronousRequest to dataTaskWithRequest我最近从 sendSynchronousRequest 切换到 dataTaskWithRequest

with sendSynchronousRequest my method was working perfectly but when I switch to dataTaskWithRequest I get the following error:使用 sendSynchronousRequest 我的方法运行良好,但是当我切换到 dataTaskWithRequest 时,出现以下错误:

error   NSURLError *    domain: @"NSURLErrorDomain" - code: 4294966096  0x15ee96c0

and

myError NSError *   domain: nil - code: 1684370017  0x26cce125

I don't understand why.我不明白为什么。

Here is the old code (commented out) and the new code:这是旧代码(注释掉)和新代码:

/*-(NSDictionary *)GetProductionScheduleData:(NSString *)areaDescription
{
    NSString *areaDescriptionWSpaceCharacters = [areaDescription stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSString *requestString = [NSString stringWithFormat:@"%@?areaDescription=%@",kIP,areaDescriptionWSpaceCharacters];
    NSURL *JSONURL = [NSURL URLWithString:requestString];
    NSURLResponse* response = nil;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    if(data == nil)
        return nil;
    NSError *myError;
    NSDictionary *productionSchedule = [[NSDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
    return productionSchedule;
}*/

-(void)GetProductionScheduleData:(NSString *)areaDescription Completetion:(void (^) (NSMutableDictionary * result,NSError * error))completion{

    NSString *areaDescriptionWSpaceCharacters = [areaDescription stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSString *requestString = [NSString stringWithFormat:@"%@?areaDescription=%@",kIP,areaDescriptionWSpaceCharacters];
    NSURL *JSONURL = [NSURL URLWithString:requestString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                                      {
                                          NSError *myError;
                                          NSMutableDictionary *productionSchedule = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];

                                          completion(productionSchedule,myError);

                                      }];
    [dataTask resume];

}

Please Help!请帮忙! This was working with sendSynchronousRequest I am starting to dislike dataTaskWithRequest.这是使用 sendSynchronousRequest 我开始不喜欢 dataTaskWithRequest。

The NSURLSession code you have is correct, I confirmed with a valid URL.您拥有的 NSURLSession 代码是正确的,我确认了一个有效的 URL。

You stopped checking to see if data is nil before attempting to JSON parse it.在尝试 JSON 解析数据之前,您停止检查数据是否为零。 If you add that check back I bet you'll find that there is an error and data is in fact nil.如果您添加该检查,我敢打赌您会发现存在错误并且数据实际上为零。

Change to:改成:

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // handle request error
    if (error) {
        completion(nil, error);
        return;
    }

    NSError *myError;
    NSMutableDictionary *productionSchedule = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
    completion(productionSchedule,myError);
}];

I would recommend also checking myError before attempting to set it to productionSchedule (which could also cause a crash).我建议在尝试将其设置为 productionSchedule 之前检查 myError (这也可能导致崩溃)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM