应用程序使用NSURLSessionDataTask

时间:2015-05-22 11:55:13

标签: ios objective-c multithreading nsoperationqueue nsurlsession

我的应用程序在NSOperationQueue中使用NSURLSessionDataTask从服务器下载数据时崩溃。 我在NSOperation类中使用以下代码。

- (void) main {
    @autoreleasepool {
        if (self.isCancelled)
            return;

        [self getGroupDetailsWithStream:_stream];
    }
}

- (void) getGroupDetailsWithStream : (CF_Stream*) stream {
    [[WebCommunication sharedInstances] getData:^(NSArray *responce, BOOL status) {
        if (status) {
            if ([[responce valueForKey:KEY_SUCCESS] integerValue] == 1) {
                if (status) {
                    _groupData = [[GroupData alloc] initWithResultSet:[responce valueForKey:KEY_GROUPPROFILE]];
                    [self setFinished:YES];
                    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(groupDownloadDidFinish:) withObject:self waitUntilDone:NO];
                }
            }
        }
    } withUrl:urlString withAuthToken:authToken];
}

#pragma mark - NSOperation methods

- (BOOL)isConcurrent
{
    return YES;
}

- (void)setExecuting:(BOOL)executing
{
    [self willChangeValueForKey:@"isExecuting"];
    _isExecuting = executing;
    [self didChangeValueForKey:@"isExecuting"];
}

- (void)setFinished:(BOOL)finished
{
    [self willChangeValueForKey:@"isFinished"];
    _isFinished = finished;
    [self didChangeValueForKey:@"isFinished"];
}

以下是WebCommunication类的代码。

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: nil];

    [self getHeaderMethodWithUrl:urlStr withAuthToken:authToken];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:mutablerequest
                                                       completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                                           [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                                                           NSArray *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                                                           if (!error && [result count] > 0) {
                                                               myCompletionBlock(result,YES);
                                                           } else {
                                                               myCompletionBlock(nil,NO);
                                                           }
                                                       }];
    [dataTask resume];

可变性要求守则

 NSURL *url = [NSURL URLWithString:urlStr];
    mutablerequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
    [mutablerequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    if ([authToken length] >0) {
        [mutablerequest setValue:authToken forHTTPHeaderField:KEY_AUTHTOKEN];
    }
    [mutablerequest setHTTPMethod:@"GET"];

我认为应用程序在返回块时会崩溃。

错误消息-__ NSCFType HTTPBodyStream无法识别的选择器

非常感谢任何帮助。谢谢!

0 个答案:

没有答案