我在这里解析 5 个 url,使用NSURLConnection delegate
它从 mysql db 检索 json 并在用户登录时将核心数据插入到 DB 中。但是代码花费了太多时间,我该如何减少这里的时间,请帮忙。
dispatch_queue_t myBackgroundQueue;
myBackgroundQueue = dispatch_queue_create("loginTask", NULL);
dispatch_async(myBackgroundQueue, ^(void){
// code for parsing 5 urls
json_parser *obj= [[json_parser alloc]init];
[obj parseUrl:@"myurl"];
});
@interface json_parser () {
NSMutableArray *array;
NSMutableData *mdata;
NSURLConnection *conn;
}
@end
@implementation json_parser
- (void)parseUrl:(NSString *)baseUrl
{
NSURL *url=[NSURL URLWithString:baseUrl];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
conn=[NSURLConnection connectionWithRequest:request delegate:self];
if(conn) {
mdata=[[NSMutableData alloc]init];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[mdata setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[mdata appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allData=[NSJSONSerialization JSONObjectWithData:mdata options:0 error:nil];
for (NSDictionary *dict in allData)
{
masterGeneric *masterObj=[[masterGeneric alloc]init];//this is nsobject type class
masterObj.masterId=[dict objectForKey:@"id"];
masterObj.genericname=[dict objectForKey:@"genericname"];
masterObj.salient_features=[dict objectForKey:@"salient_features"];
masterObj.bioactivity_group=[dict objectForKey:@"bioactivity_group"];
masterObj.therapy_group=[dict objectForKey:@"therapy_group"];
masterObj.dosage_information=[dict objectForKey:@"dosage_information"];
masterObj.indications=[dict objectForKey:@"indications"];
masterObj.food=[dict objectForKey:@"food"];
masterObj.interaction=[dict objectForKey:@"interaction"];
masterObj.side_effects=[dict objectForKey:@"side_effects"];
masterObj.precautions=[dict objectForKey:@"precautions"];
masterObj.warnings=[dict objectForKey:@"warnings"];
masterObj.advice_to_patients=[dict objectForKey:@"advice_to_patients"];
masterObj.route_of_administration=[dict objectForKey:@"route_of_administration"];
masterObj.available_form=[dict objectForKey:@"available_form"];
}
}