0

我似乎遇到了麻烦。出于某种原因,如果我尝试检查NSURLSessionTask对象的类类型,它根本不起作用。如果我检查它们的taskDescription属性,如果我在启动任务之前设置它们,这当然有效。我只想知道为什么下面的代码对我不起作用。感谢您提供的任何帮助!

- (void)uploadIt
{
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    defaultConfigObject.timeoutIntervalForResource = 15.0;
    defaultConfigObject.requestCachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURLSessionUploadTask *uploadTask = [defaultSession uploadTaskWithRequest:someRequest fromData:body];
    [uploadTask resume];
}  

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if (self.uploadResponseData)
    {
        NSDictionary *d = [NSJSONSerialization JSONObjectWithData:self.uploadResponseData options:kNilOptions error:nil];
        NSLog(@"Dict: %@",d);
    }

    if ([task isKindOfClass:[NSURLSessionDownloadTask class]])
    {
        // Not called
    }
    else if ([task isKindOfClass:[NSURLSessionUploadTask class]])
    {
        // Not called
    }
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    if (!self.uploadResponseData)
    {
        self.uploadResponseData = [NSMutableData dataWithData:data];
    }
    else
    {
        [self.uploadResponseData appendData:data];
    }
}
4

2 回答 2

4

我在这方面的经验是在 app-in-the-background的背景下进行NSURLSessionTask的。这些被序列化到磁盘上,涉及一个守护进程,我的经验是,任何基于类的欺骗在这种情况下都不能很好地工作。

特别是,我试图子类NSURLSessionTask化,但-didComplete:...调用中返回的不是该子类的实例。

我开始认为NSURLSessionTask它是一个门面,或者是“类集群”的一部分,并试图更加小心。

如果您需要将任务标记为特定类型,请将您喜欢的任何内容存储在.description. 我可以确认信息很好地在后台序列化中幸存下来。

于 2014-08-22T16:00:53.330 回答
1

我无法从您发布的代码中重现该问题。我从您的代码中做了一个最小的示例,只是从标准 Xcode 模板创建了一个单视图应用程序,然后将其用作视图控制器:

视图控制器.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <NSURLSessionDelegate, NSURLSessionTaskDelegate>

@end

视图控制器.m:

#import "ViewController.h"

@interface ViewController ()


@end

@implementation ViewController

 1. (void)viewDidLoad {
    [super viewDidLoad];
    [self uploadIt];
}

 2. (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 3. (void)uploadIt
{
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    defaultConfigObject.timeoutIntervalForResource = 15.0;
    defaultConfigObject.requestCachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURLRequest* someRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://example.com"]];
    NSData* body = [@"Fake body" dataUsingEncoding:NSUTF8StringEncoding];

    NSURLSessionUploadTask *uploadTask = [defaultSession uploadTaskWithRequest:someRequest fromData:body];
    [uploadTask resume];
}

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
    NSLog(@"Completed.");

    if ([task isKindOfClass:[NSURLSessionDownloadTask class]])
    {
        NSLog(@"The task was a download task");
    }
    else if ([task isKindOfClass:[NSURLSessionUploadTask class]])
    {
        NSLog(@"The task was an upload task");
    }
}
@end

结果如下:

2014-07-02 17:57:28.002 24500545_nsurlsession_classes[22470:1250475] 完成。2014-07-02 17:57:48.631 24500545_nsurlsession_classes[22470:1250475] 任务是上传任务

(您确定您的日志记录正确吗?如果您在委托方法中设置断点并单步执行代码,您会看到什么p task?在 lldb 中告诉您什么?)

于 2014-07-02T17:02:50.040 回答