I have made an application for IOS, in which I am using sqlite database, database is local in application.
Now I have given the functionality the application is downloading data from internet & put it in local database & show infront of user. I have given this functionality on - (void)viewDidLoad
so that when application is downloading data, it stop working till it finish downloading part, for this user need to wait to interact with application.
Now I wants to functionality a thread run in background of application which will connect the internet & update the application without interfering user. please help me.
My code of download & save image is this:
-(void)Download_save_images:(NSString *)imagesURLPath :(NSString *)image_name
{
NSMutableString *theString = [NSMutableString string];
// Get an image from the URL below
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:NSURL URLWithString:imagesURLPath]]];
NSLog(@"%f,%f",image.size.width,image.size.height);
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// If you go to the folder below, you will find those pictures
NSLog(@"%@",docDir);
[theString appendString:@"%@/"];
[theString appendString:image_name];
NSLog(@"%@",theString);
NSLog(@"saving png");
NSString *pngFilePath = [NSString stringWithFormat:theString,docDir];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:YES];
NSLog(@"saving image done");
[image release];
// [theString release];
}
when i am debugging application i seen my application taking more time at below line:
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:NSURL URLWithString:imagesURLPath]]];