简体   繁体   English

如何在AFNetworking 2.0中使用Progress参数

[英]How to use Progress parameter in AFNetworking 2.0

I am trying to use AFNetworking 2.0 with NSURLSession. 我正在尝试将AFNetworking 2.0与NSURLSession一起使用。 I am using the method 我正在使用这种方法

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
                                         fromFile:(NSURL *)fileURL
                                         progress:(NSProgress * __autoreleasing *)progress
                                completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler;

How am I supposed to use the progress parameter. 我该如何使用progress参数。 The method is a non blocking method. 该方法是一种非阻塞方法。 Hence I will have to listen to the ' progress ' to get the updates. 因此,我将不得不听取“ progress ”以获得更新。 But the parameter wouldn't take a property. 但参数不会占用属性。 Only takes a local variable(NSProgress * __autoreleasing *). 只接受局部变量(NSProgress * __autoreleasing *)。 I can't add KVO to a local var. 我无法将KVO添加到本地var。

I am not really sure how to use. 我不太确定如何使用。

Any time an argument is given as ** it means that you're supposed to pass in the pointer to the pointer to an existing object, not a pointer to the actual object as you would normally do. 任何时候一个参数都是**这意味着你应该将指针传递给指向现有对象的指针,而不是像往常那样传递给实际对象的指针。

In this case, you pass in a pointer to a pointer to an NSProgress object and then observe the changes in that object in order to get the updates. 在这种情况下,您传入指向NSProgress对象的指针,然后观察该对象中的更改以获取更新。

Example: 例:

// Create a progress object and pass it in
NSProgress *progress;
[sessionManager uploadTaskWithRequest:request fromFile:fileURL progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    // Completion code
}];

// Observe fractionCompleted using KVO
[progress addObserver:self
          forKeyPath:@"fractionCompleted"
             options:NSKeyValueObservingOptionNew
             context:NULL];

Then it gets reported in: 然后报告:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    if ([keyPath isEqualToString:@"fractionCompleted"] && [object isKindOfClass:[NSProgress class]]) {
        NSProgress *progress = (NSProgress *)object;
        NSLog(@"Progress is %f", progress.fractionCompleted);
    }
}

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

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