So This code is working for me before iOS8, I have tested it on 3000 Images download loop in iOS7.1.
But recently some of App user Compliant me about App Crash While Image Download Process.
Using this way now Just 200 Images are downloading and App Crashes.
I just figured Out On every Image download There is about 0.6 to 0.8 MB Memory is increasing and after about 200 Images App Crashes.
I have to Download Images Synchronously, Because when i try to download in Background Thread or Asynchronous mode or even try to Use dispatch_async(dispatch_get_main_queue() way. The Process Start after some seconds and till then the While loop i am using just reach to its end.
So Please help me on this issue. What should i do to resolve it.
-(void) downloadImages
{
NSString *myDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
database = [dBName UTF8String];
if (sqlite3_open(database, &dbConnection) == SQLITE_OK)
{
sqliteQuery = [NSString stringWithFormat: @"SELECT ProductID FROM ProductDetails WHERE ImageDownloadStatus = 0"];
int i = 0;
if (sqlite3_prepare_v2(dbConnection, [sqliteQuery UTF8String], -1, &sQLStatement, NULL) == SQLITE_OK)
{
while (sqlite3_step(sQLStatement) == SQLITE_ROW)
{
@autoreleasepool
{
temp = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 0)];
NSString *imageURL = [NSString stringWithFormat:@"http://ift.tt/1vajKBY",clientSite,temp];
myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
NSString *imagePath = [NSString stringWithFormat:@"%@/%@.jpg",myDirectory,temp];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(myImage, 1.0f)];
[imageData writeToFile:imagePath atomically:YES];
sqliteQuery2 = [NSString stringWithFormat: @"UPDATE ProductDetails SET ImageDownloadStatus = 1 WHERE ProductID = \'%@\'",temp];
if (sqlite3_prepare_v2(dbConnection, [sqliteQuery2 UTF8String], -1, &sQLStatement2, NULL) == SQLITE_OK)
{
if (sqlite3_step(sQLStatement2) == SQLITE_DONE)
sqlite3_finalize(sQLStatement2);
}
[self performSelectorInBackground:@selector(updateUILabels) withObject:nil];
i = i + 1;
}
}
sqlite3_finalize(sQLStatement);
}
sqlite3_close(dbConnection);
}
}
-(void) updateUILabels
{
@autoreleasepool
{
progressCounter.hidden = NO;
lblProgressStatus.hidden = NO;
spinner.hidden = NO;
[spinner startAnimating];
lblProgressStatus.text = @"Synchronising Product Images...";
lblProgressCounter.text = [NSString stringWithFormat:@"%d / %d”, i,total];
}
}
Aucun commentaire:
Enregistrer un commentaire