检查发生了哪种NSURLSessionTask

时间:2014-06-30 23:47:37

标签: ios objective-c nsurlsession nsurlsessiontask nsurlsessionuploadtask

我好像遇到了麻烦。出于某种原因,如果我尝试检查我的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];
    }
}

2 个答案:

答案 0 :(得分:4)

我对此的体验是在app-in-the-background NSURLSessionTask的背景下。这些序列化为磁盘,涉及到一个deamon,我的经验是任何基于类的finagling在这种情况下都不能很好地工作。

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

我认为NSURLSessionTask是一个门面,或者是“类群”的一部分,并试图对此更加谨慎。

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

答案 1 :(得分:1)

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

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <NSURLSessionDelegate, NSURLSessionTaskDelegate>

@end

ViewController.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中告诉你什么?)