0

是否有任何通用在此处输入图像描述方法来修复 ios 应用程序中的这种内存泄漏。

我正在从服务器获取 5000 张图像,然后将其保存在文档目录中,同时将它们的路径保存在核心数据中。

这是代码

-(void)connectionDidFinishLoading:(NSURLConnectionWithTag *)connection
{
    NSNumber* nn66 =  [NSNumber numberWithInt:2];

    if ([nn66 isEqualToNumber:connection.tag])
    {
        NSXMLDocument* rElement  = [[NSXMLDocument alloc] initWithData:[dataFromConnectionsByTag objectForKey:connection.tag] options:0 error:nil];
        NSXMLElement* element    = [rElement rootElement];

        NSArray*  shadowElement3 = [element elementsForName:@"PRODUCT_NAME"];

        for (int i = 0; i <  [shadowElement3 count]; i++)
        {
            @autoreleasepool
            {
                NSMutableArray* array = [[NSMutableArray alloc] init];

                Product* failedBankInfo = [NSEntityDescription insertNewObjectForEntityForName:@"Product"                                                                    inManagedObjectContext:context];

                NSXMLElement* element11 = [shadowElement3 objectAtIndex:i];

                NSString* strPath = [element11 stringValueForNode:@"B1_COD"];

                NSArray *paths                =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
                NSString *documentsDirectory  =  [paths objectAtIndex:0];
                NSString *savedImagePath      =  [documentsDirectory stringByAppendingPathComponent:strPath];

                [array        addObject:savedImagePath];

                if (getimages == NO)
                {
                    //**************************************************
                    NSData* receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[element11 stringValueForNode:@"img"]]];
                    [receivedData writeToFile:savedImagePath atomically:YES];
                    //**************************************************

                    //**************************************************
                    int testNo = ((i+1)*100)/[shadowElement3 count];
                    NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:[tableDataArray objectAtIndex:lineNo]];
                    [dic setObject:[NSString stringWithFormat:@"%d",[[NSNumber numberWithInt:testNo] intValue]] forKey:@"status"];
                    [dic setObject:[NSString stringWithFormat:@"%@",[self onlyDateFromDate:[NSDate date]]]      forKey:@"syncDate"];
                    [dic setObject:[NSString stringWithFormat:@"%d/%d",[[NSNumber numberWithInt:i+1] intValue],[[NSNumber numberWithInt:[shadowElement3 count]] intValue]] forKey:@"totalloaded"];

                    [tableDataArray replaceObjectAtIndex:lineNo withObject:dic];
                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"settingdata"];
                    [[NSUserDefaults standardUserDefaults] setObject:tableDataArray forKey:@"settingdata"];
                    [_mtable reloadData];
                   // [self performSelectorInBackground:@selector(ricaelleupdatecompleted:) withObject:dic];

                    [NSThread detachNewThreadSelector:@selector(ricaelleupdatecompleted:) toTarget:self withObject:dic];
                    //**************************************************
                }
                else
                {
                    NSString* newImagePath = [NSString stringWithFormat:@"%@_t",strPath];
                    NSString *savedImagePath1      =  [documentsDirectory stringByAppendingPathComponent:newImagePath];


                    [array        addObject:savedImagePath1];

                    int testNo = ((i+1)*100)/[shadowElement3 count];
                    NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithCapacity:0];
                    [dic setObject:[NSString stringWithFormat:@"%d%@ Completo",[[NSNumber numberWithInt:testNo] intValue],@"%"] forKey:@"status"];
                    [dic setObject:[NSString stringWithFormat:@"%@",[self onlyDateFromDate:[NSDate date]]]      forKey:@"syncDate"];
                    [dic setObject:[NSString stringWithFormat:@"%d/%d",[[NSNumber numberWithInt:i+1] intValue],[[NSNumber numberWithInt:[shadowElement3 count]] intValue]] forKey:@"totalloaded"];
                    [NSThread detachNewThreadSelector:@selector(allimagesCompleted:) toTarget:self withObject:dic];
                }

                failedBankInfo.images = array;
                failedBankInfo.DA1_FILIAL = [element11 stringValueForNode:@"DA1_FILIAL"];
                failedBankInfo.DA1_CODTAB = [element11 stringValueForNode:@"DA1_CODTAB"];
                failedBankInfo.DA1_CODPRO = [element11 stringValueForNode:@"DA1_CODPRO"];
                failedBankInfo.DA1_ITEM   = [element11 stringValueForNode:@"DA1_ITEM"];
                failedBankInfo.DA1_PRCVEN = @"100"; //[element11 stringValueForNode:@"DA1_PRCVEN"];
                failedBankInfo.DA1_X_PROM = @"10"; //[element11 stringValueForNode:@"DA1_X_PROM"];
                failedBankInfo.DA1_X_ALTE = [element11 stringValueForNode:@"DA1_X_ALTE"];
                failedBankInfo.DA1_X_PRFI = [element11 stringValueForNode:@"DA1_X_PRFI"];
                failedBankInfo.B2_FILIAL  = [element11 stringValueForNode:@"B2_FILIAL"];
                failedBankInfo.B2_COD     = [element11 stringValueForNode:@"B2_COD"];
                failedBankInfo.B2_LOCAL   = [element11 stringValueForNode:@"B2_LOCAL"];
                failedBankInfo.B2_QATU    = @"25";// [element11 stringValueForNode:@"B2_QATU"];
                failedBankInfo.B1_FILIAL  = [element11 stringValueForNode:@"B1_FILIAL"];
                failedBankInfo.B1_COD     = [element11 stringValueForNode:@"B1_COD"];
                failedBankInfo.B1_DESC    = strPath;
                failedBankInfo.B1_TIPO    = [element11 stringValueForNode:@"B1_TIPO"];
                failedBankInfo.B1_UM      = [element11 stringValueForNode:@"B1_UM"];
                failedBankInfo.B1_LOCPAD  = [element11 stringValueForNode:@"B1_LOCPAD"];
                failedBankInfo.B1_GRUPO   = [element11 stringValueForNode:@"B1_GRUPO"];
                failedBankInfo.B1_IPI     = [element11 stringValueForNode:@"B1_IPI"];
                failedBankInfo.B1_POSIPI  = [element11 stringValueForNode:@"B1_POSIPI"];
                failedBankInfo.B1_ALTER   = [element11 stringValueForNode:@"B1_ALTER"];
                failedBankInfo.B1_X_ALT_P = [element11 stringValueForNode:@"B1_X_ALT_P"];
                failedBankInfo.B1_X_LAR_P = [element11 stringValueForNode:@"B1_X_LAR_P"];
                failedBankInfo.B1_X_COM_P = [element11 stringValueForNode:@"B1_X_COM_P"];
                failedBankInfo.B1_PESO    = [element11 stringValueForNode:@"B1_PESO"];
                failedBankInfo.B1_PESBRU  = [element11 stringValueForNode:@"B1_PESBRU"];
                failedBankInfo.B1_X_ALT_C = [element11 stringValueForNode:@"B1_X_ALT_C"];
                failedBankInfo.B1_X_LAR_C = [element11 stringValueForNode:@"B1_X_LAR_C"];
                failedBankInfo.B1_X_COM_C = [element11 stringValueForNode:@"B1_X_COM_C"];
                failedBankInfo.B1_X_DIA_P = [element11 stringValueForNode:@"B1_X_DIA_P"];
                failedBankInfo.B1_X_DIA_C = [element11 stringValueForNode:@"B1_X_DIA_C"];
                failedBankInfo.B1_X_PLI_C = [element11 stringValueForNode:@"B1_X_PLI_C"];
                failedBankInfo.B1_X_PBR_C = [element11 stringValueForNode:@"B1_X_PBR_C"];
                failedBankInfo.B1_X_ALT_M = [element11 stringValueForNode:@"B1_X_ALT_M"];
                failedBankInfo.B1_X_LAR_M = [element11 stringValueForNode:@"B1_X_LAR_M"];
                failedBankInfo.B1_X_COM_M = [element11 stringValueForNode:@"B1_X_COM_M"];
                failedBankInfo.B1_X_DIA_M = [element11 stringValueForNode:@"B1_X_DIA_M"];
                failedBankInfo.B1_X_PBR_M = [element11 stringValueForNode:@"B1_X_PBR_M"];
                failedBankInfo.B1_X_PLI_M = [element11 stringValueForNode:@"B1_X_PLI_M"];
                failedBankInfo.B1_X_PGCAT = [NSNumber numberWithInt:[[element11 stringValueForNode:@"B1_X_PGCAT"] intValue]];
                failedBankInfo.B1_X_CXMAS = [element11 stringValueForNode:@"B1_X_CXMAS"];
                failedBankInfo.B1_CODBAR  = [element11 stringValueForNode:@"B1_CODBAR"];
                failedBankInfo.B1_X_DUN14 = [element11 stringValueForNode:@"B1_X_DUN14"];
                failedBankInfo.B1_TS      = [element11 stringValueForNode:@"B1_TS"];
                failedBankInfo.B1_TE      = [element11 stringValueForNode:@"B1_TE"];
                failedBankInfo.B1_X_DESCR = [element11 stringValueForNode:@"B1_X_DESCR"];
                failedBankInfo.B1_QE      =  [NSNumber numberWithInt:4];     //[[element11 stringValueForNode:@"B1_QE"] intValue]]; //[eleme

                NSError *error;

                if (![context save:&error])
                {
                    NSLog(@"error occured");
                }
                else
                {
                    NSLog(@"clientes Saved copupons");
                }
            }
        }
    }

    [SVProgressHUD dismiss];

    if (getimages == NO)
    {
        [SVProgressHUD showWithStatus:@"Please wait we are processing images"];
        [self performSelector:@selector(complieImage) withObject:nil afterDelay:.1];
    }



    NSNumber* nnn =  [NSNumber numberWithInt:3];
    if ([connection.tag isEqualToNumber:nnn])
    {
        [self  saveClientTableData:nnn];
    }

    NSNumber* nn5 =  [NSNumber numberWithInt:5];
    if ([nn5 isEqualToNumber:connection.tag])
    {
        [self saveBannerData:nn5];
    }

    [SVProgressHUD dismiss];
    [Utils readAllCoreData];
    [dataFromConnectionsByTag removeObjectForKey:connection.tag];
}
4

0 回答 0