简体   繁体   中英

Custom Delegate is not calling from NSURLSessionDelegates

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.

-(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.

Just add your service model object as delegate. Eg assume your servcice model class is MyModel and your posted code is in another class MyDownloader. In your MyModel class add:

MyDonlowder *downlowder = [[MyDownloader alloc] init];
downlowder.delegate = self;

Also make your MyModel class conform to MyCystomDelegateProtocol and implement the required method updateUploadedDataInfo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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