How to use Progress parameter in AFNetworking 2.0

寵の児 提交于 2019-11-29 03:52:47
iwasrobbed

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.

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!