简体   繁体   English

iOS8,目标C中的内存管理问题

[英]Memory Management Issue in iOS8, Objective C

So This code was working for me before iOS 8, I have tested it on 3000 Images download loop in iOS 7.1. 因此,此代码在iOS 8之前对我有用,我已经在iOS 7.1的3000 Images下载循环中对其进行了测试。

But recently some of my App user Compliant me about App Crash While Image Download Process. 但是最近,我的一些App用户使我对图像下载过程中的App崩溃感到满意。

Using this way now Just 200 Images are downloading and App Crashes. 现在使用这种方式下载的只是200张图片,并且应用程序崩溃。

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. 我刚刚弄清楚,每次下载图像时,内存大约在0.6到0.8 MB之间,并且在出现大约200张图像应用程序崩溃后才增加。

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. 我必须同步下载图像,因为当我尝试在Background ThreadAsynchronous mode下载,甚至尝试使用dispatch_async(dispatch_get_main_queue()方式)时,该过程将在几秒钟后开始,直到那时我使用的While循环才到达其结束。

So Please help me on this issue. 因此,请在这个问题上帮助我。 What should i do to resolve it. 我应该怎么做才能解决它。

My code for download Images is: 我下载图片的代码是:

-(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://xxxxxxxxxxxxxxx.jpg",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];       
}
}

You should execute the downloadImages method in the background and the updatedUILabels method in the main thread. 您应该在后台执行downloadImages方法,并在主线程中执行updatedUILabels方法。 You can use dispatch_async(dispatch_get_main_queue(), ^{ [self updateUILabels]; }); 您可以使用dispatch_async(dispatch_get_main_queue(), ^{ [self updateUILabels]; }); or [self performSelectorOnMainThread:@selector(updateUILabels:) withObject:nil waitUntilDone:NO]; [self performSelectorOnMainThread:@selector(updateUILabels:) withObject:nil waitUntilDone:NO]; the execute updatedUILabels in the main thread. 在主线程中执行updatedUILabels

In general always execute long running operations in an background thread (either using GCD, NSOperations or blocks) and always execute functions that update your UI on the main thread. 通常,始终在后台线程中执行长时间运行的操作(使用GCD,NSOperations或块),并始终执行在主线程上更新UI的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM