我遇到了NSUrlSession上传任务问题。我上传的图像间隔为5分钟,后端设置的代码(php)。当我点击放置在表格单元格上的上传按钮时,我呼叫通知中心执行上传任务。我第一次上传时工作正常,但上传后当我尝试在一个minuts中上传图片时,它会多次调用它的代理。
cell.m中的按钮操作代码:
- (IBAction)uploadnewBtn:(id)sender {
touchText.alpha = 0;
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:[UIApplication sharedApplication]];
[UIView setAnimationDidStopSelector:@selector(endIgnoringInteractionEvents)];
touchText.alpha = 1;
[UIView commitAnimations];
[[NSNotificationCenter defaultCenter] postNotificationName:@"uploadBoastsDataBoaster" object:nil userInfo:nil];
}
通知中心方法代码:
-(void)uploadBoastsDataBoaster
{
NSLog(@"Counter >>>>>>>>>>>>>");
[self.tableView1 reloadData];
NSIndexSet * sections = [NSIndexSet indexSetWithIndex:0];
[self.tableView1 reloadSections:sections withRowAnimation:UITableViewRowAnimationNone];
NSLog(@"value:%@",model);
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
NSLog(@"There IS NO internet connection");
CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle];
style.messageColor = [UIColor redColor];
[self.view makeToast:@"There IS NO internet connection."
duration:3.0
position:CSToastPositionCenter
style:style];
[CSToastManager setSharedStyle:style];
} else {
[self OnSubmitImg:multipleImageData];
}
最终上传任务方法:
-(void)OnSubmitImg:(NSMutableDictionary*)image
{
NSOperationQueue
*myQueue = [[NSOperationQueue alloc] init];
// This creates basically a serial queue, since there is just on operation running at any time.
[myQueue setMaxConcurrentOperationCount:1];
NSURL *url = [NSURL URLWithString:@"url"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:25.0];
[request setHTTPMethod:@"POST"];
NSString *contentTypeValue = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentTypeValue forHTTPHeaderField:@"Content-type"];
NSDictionary* dictionary = [[NSDictionary alloc]init];
NSString* uid = [[NSUserDefaults standardUserDefaults]objectForKey:kUser_id];
// NSString *uid = [[NSUserDefaults standardUserDefaults] valueForKey:@"FBid"]
dictionary = @{@"user_id": fbEmail,@"fileToUpload[]":image};
NSMutableData *dataForm = [NSMutableData alloc];
for (NSString *param in [dictionary allKeys])
{
if([param isEqualToString:@"fileToUpload[]"])
{
for(int i=0;i<multipleImageDataObjects.count;i++)
{
UIImage *image = [multipleImageDataObjects objectAtIndex:i];
NSData *imageData = UIImageJPEGRepresentation(image, 0.3f);
[dataForm appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[dataForm appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileToUpload[]\"; filename=\"%@%d.jpg\"\r\n", @"test", i] dataUsingEncoding:NSUTF8StringEncoding]];
[dataForm appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[dataForm appendData:[NSData dataWithData:imageData]];
[dataForm appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
else
{
[dataForm appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[dataForm appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[dataForm appendData:[[NSString stringWithFormat:@"%@", [dictionary objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
[dataForm appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
NSURLSessionUploadTask *uploadTask = [urlSession uploadTaskWithRequest:request fromData:dataForm];
[uploadTask resume];
}