I am using NSURLSession for service integration. While uploading an image in to server, I want to show the percentage of data that has been uploaded to server. For that, i created a custom delegate in my service model class, and calling this delegate inside NSURLSession's delegate. Please check my code below.
我正在使用NSURLSession进行服务集成。在将图像上传到服务器时,我想显示已上载到服务器的数据百分比。为此,我在服务模型类中创建了一个自定义委托,并在NSURLSession的委托中调用此委托。请检查下面的代码。
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
NSLog(@" %lld %lld %lld %lu", bytesSent , totalBytesSent,totalBytesExpectedToSend,(unsigned long)task.taskIdentifier);
float dataSent = (totalBytesSent/totalBytesExpectedToSend);
NSLog(@"sent percentage : %0.2f",(dataSent * 100));
dispatch_async(dispatch_get_main_queue(), ^{
//_delegate is my custom delegate
[_delegate updateUploadedDataInfo:dataSent];
});
}
But, [_delegate updateUploadedDataInfo:dataSent] method is not calling here. Please suggest, where i am doing wrong.
但是,[_delegate updateUploadedDataInfo:dataSent]方法不在这里调用。请建议我在哪里做错了。
1 个解决方案
#1
1
Just add your service model object as delegate. E.g assume your servcice model class is MyModel and your posted code is in another class MyDownloader. In your MyModel class add:
只需将您的服务模型对象添加为委托即可。例如,假设您的servcice模型类是MyModel,并且您发布的代码位于另一个类MyDownloader中。在你的MyModel类中添加:
MyDonlowder *downlowder = [[MyDownloader alloc] init];
downlowder.delegate = self;
Also make your MyModel class conform to MyCystomDelegateProtocol and implement the required method updateUploadedDataInfo
.
还要使MyModel类符合MyCystomDelegateProtocol并实现所需的方法updateUploadedDataInfo。
#1
1
Just add your service model object as delegate. E.g assume your servcice model class is MyModel and your posted code is in another class MyDownloader. In your MyModel class add:
只需将您的服务模型对象添加为委托即可。例如,假设您的servcice模型类是MyModel,并且您发布的代码位于另一个类MyDownloader中。在你的MyModel类中添加:
MyDonlowder *downlowder = [[MyDownloader alloc] init];
downlowder.delegate = self;
Also make your MyModel class conform to MyCystomDelegateProtocol and implement the required method updateUploadedDataInfo
.
还要使MyModel类符合MyCystomDelegateProtocol并实现所需的方法updateUploadedDataInfo。