मैं प्रोग्रामेटिक रूप से एक एक्सएलएस शीट बनाने की कोशिश कर रहा हूं। शीट भरने के लिए, मैं कई NSURLConnection
लगभग 100 बना रहा हूं। अभी मेरा दृष्टिकोण है:एकाधिक NSURL कनेक्शन को संभालने का सबसे अच्छा तरीका
- कनेक्शन बनाएं और डेटा को सरणी में संग्रहीत करें। इस सरणी में 100 वस्तुएं हैं।
- अब पहली वस्तु लें और कनेक्शन को कॉल करें। डेटा स्टोर करें। और सरणी में दूसरी वस्तु के साथ दूसरा कनेक्शन बनाओ। यह सरणी में अंतिम वस्तु तक जारी है।
100 कनेक्शन समाप्त करने में औसत 14 सेकंड लगते हैं। प्रतिक्रिया को तेजी से प्राप्त करने के लिए NSURLConnection
को लागू करने का कोई तरीका है?
गुण की घोषणा:
@property (nonatomic,strong) NSURLConnection *getReportConnection;
@property (retain, nonatomic) NSMutableData *receivedData;
@property (nonatomic,strong) NSMutableArray *reportArray;
सरणी आरंभ किया जा रहा viewDidLoad
में:
reportArray=[[NSMutableArray alloc]init];
एक बटन कार्रवाई में NSURLConnection
शुरु कर रहा है:
मैं जैसे बुनियादी दृष्टिकोण का पालन किया
/initialize url that is going to be fetched.
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"****/%@/crash_reasons",ID]];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:tokenReceived forHTTPHeaderField:@"**Token"];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//initialize a connection from request
self.getReportConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
प्राप्त डेटा का संसाधन: एक पूर्ण होने के बाद किसी अन्य कनेक्शन कॉलिंग
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data{
if (connection==_getVersionConnection) {
[self.receivedData_ver appendData:data];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];
[JSON[@"app_versions"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (![obj[@"id"] isEqual:[NSNull null]] && ![reportArray_ver containsObject:obj[@"id"]]) {
[reportArray_ver addObject:obj[@"id"]];
}
NSLog(@"index = %lu, Object For title Key = %@", (unsigned long)idx, obj[@"id"]);
}];
if (JSON!=nil) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Version Reports succesfully retrieved" message:@"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}
}
:
// This method is used to process the data after connection has made successfully.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (connection==getReportConnection) {
//check and call the connection again
}
}
और आज, मैं अन्य पाश उपयोग करने के बाद सभी कनेक्शनों एक आग sendAsync
साथ NSURLConnection
की कोशिश की , और यह बहुत अच्छी तरह से काम किया।
self.receivedData_ver=[[NSMutableData alloc]init];
__block NSInteger outstandingRequests = [reqArray count];
for (NSString *URL in reqArray) {
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
[self.receivedData appendData:data]; //What is the use of appending NSdata into Nsmutable data?
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];
NSLog(@"login json is %@",JSON);
[JSON[@"app_versions"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (![obj[@"id"] isEqual:[NSNull null]] && ![reportArray_ver containsObject:obj[@"id"]]) {
[reportArray_ver addObject:obj[@"id"]];
}
NSLog(@"index = %lu, Object For title Key = %@", (unsigned long)idx, obj[@"id"]);
}];
outstandingRequests--;
if (outstandingRequests == 0) {
//all req are finished
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Version Reports succesfully retrieved" message:@"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}];
}
इस समय यह पुरानी प्रक्रिया की तुलना में 100 अनुरोध को पूरा करने के आधे समय ले लिया है, वहाँ किसी भी तेज़ तरीका मौजूद है asynReq के अलावा अन्य है? .क्या सबसे अच्छा परिदृश्य NSURLconnection
और NSURLConnection with asyncReq
उपयोग करने के लिए है?
NSURL सत्र का उपयोग क्यों नहीं करते? NSURLConnection अब –