为什么应用程序在堆镜头分析期间显示低内存警告

时间:2012-02-29 13:06:27

标签: iphone ios memory-management instruments

我正在开发一个应用程序,其中重复发生的事务被插入数据库。而我正在进行堆镜头分析,它在插入数据时显示低内存警告。大约插入1200条记录。插入的代码如下。

-(void)generateReccuringEntry:(int)tranId withAllDate:(BOOL)isAll
{

    NSMutableArray *arrDates = [[NSMutableArray alloc] init] ;
     NSArray *arr = [[_dicSaveData valueForKey:@"tran_repeatd"] componentsSeparatedByString:@" "];
    int day;
      if ([self getDayWithDayMonth:[arr objectAtIndex:1]] == 0) {
        day = [self getDay:[arr objectAtIndex:1]];
    }
    else{
        day = 1;
    }

    NSDate *startDate = [_dicSaveData valueForKey:@"tran_date"];
     NSString *strEndDate = [NSString  stringWithFormat:@"%@",[_dicSaveData valueForKey:@"tran_enddate"]];
   NSDate *endDate = nil;
    strEndDate = [strEndDate stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
  if (![strEndDate stringIsEmpty]) {
        endDate = [_dicSaveData valueForKey:@"tran_enddate"];       
    }
    else {
        //if end date not selected
        endDate = [[startDate dateByAddingYear:1]lastOfYearDate];
    }

    //get start recurring day
    NSString *strDay = [[arr lastObject] stringByReplacingOccurrencesOfString:@"()" withString:@""];
    //check start recuring day selected or not
    if ([strDay hasPrefix:@"("] && [strDay hasSuffix:@")"]) 
    {
        startDate = [self getStartDay:[arr lastObject] withStartingDate:startDate];
        if ([startDate compare:endDate]==NSOrderedDescending || [startDate compare:endDate]==NSOrderedSame) 
        {
            SafeRelease(arrDates);
            return;
        }
        if (isAll) 
        {

            NSError *error = nil;

            NSString *strQuery = [NSString stringWithFormat:@"insert into recuring values (NULL,%i,%@,%@,'%@',%@,'%@','','%@','%@',%i,1,'%@',0)",tranId,[_dicSaveData valueForKey:@"cat_id"],[_dicSaveData valueForKey:@"subcat_id"],_txtDesc.text,_txtAmount.text,sDate,[_dicSaveData valueForKey:@"tran_repeatd"],eDate,_flagForever,[_dicSaveData valueForKey:@"categoryText"]];

            strQuery = [strQuery stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
            [NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error];
            if (error) {
                [AppDelegate  showAlert:[error description] withTitle:@"Error!"];
            }
           }
        else{
            NSDate *currentDate = [[NSDate date] getDateWithDeviceTimeZone];
            if ([startDate compare:currentDate]== NSOrderedDescending) 
            {



                 NSError *error = nil;

                NSString *strQuery = [NSString stringWithFormat:@"insert into recuring values (NULL,%i,%@,%@,'%@',%@,'%@','','%@','%@',%i,1,'%@',0)",tranId,[_dicSaveData valueForKey:@"cat_id"],[_dicSaveData valueForKey:@"subcat_id"],_txtDesc.text,_txtAmount.text,sDate,[_dicSaveData valueForKey:@"tran_repeatd"],eDate,_flagForever,[_dicSaveData valueForKey:@"categoryText"]];

                strQuery = [strQuery stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
                [NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error];
                if (error) {
                    [AppDelegate  showAlert:[error description] withTitle:@"Error!"];
                }

            }
        }
    }

    int index = 2;

    if (day == 1) {
        index = 1;
    }
    do {
       if ([[arr objectAtIndex:index] isEqualToString:@"Week"]) 
        {
            startDate = [startDate dateByAddingWeek:day];

        }
        else if([[arr objectAtIndex:index] isEqualToString:@"Month"])
        {
            startDate = [startDate dateByAddingMonth:day];

        }
        else 
        {
            startDate = [startDate dateByAddingDays:day];
        }
        if (isAll) 
        {


             NSError *error = nil;


            NSString *strQuery = [NSString stringWithFormat:@"insert into recuring values (NULL,%i,%@,%@,'%@',%@,'%@','','%@','%@',%i,1,'%@',0)",tranId,[_dicSaveData valueForKey:@"cat_id"],[_dicSaveData valueForKey:@"subcat_id"],_txtDesc.text,_txtAmount.text,sDate,[_dicSaveData valueForKey:@"tran_repeatd"],eDate,_flagForever,[_dicSaveData valueForKey:@"categoryText"]];

            strQuery = [strQuery stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
            [NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error];
            if (error) {
                [AppDelegate  showAlert:[error description] withTitle:@"Error!"];
            }


        }
        //add recuring for only for future dates
        else
        {
            NSDate *currentDate = [[NSDate date] getDateWithDeviceTimeZone];
            if ([startDate compare:currentDate]== NSOrderedDescending) 
            {

                NSError *error = nil;

                NSString *strQuery = [NSString stringWithFormat:@"insert into recuring values (NULL,%i,%@,%@,'%@',%@,'%@','','%@','%@',%i,1,'%@',0)",tranId,[_dicSaveData valueForKey:@"cat_id"],[_dicSaveData valueForKey:@"subcat_id"],_txtDesc.text,_txtAmount.text,sDate,[_dicSaveData valueForKey:@"tran_repeatd"],eDate,_flagForever,[_dicSaveData valueForKey:@"categoryText"]];

                strQuery = [strQuery stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
                [NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error];
                if (error) {
                    [AppDelegate  showAlert:[error description] withTitle:@"Error!"];
                }



            }
        }

    } while ([startDate compare:endDate]==NSOrderedAscending );
      SafeRelease(arrDates);

}

can anyone tell that what is the problem with this code.so that its consuming so much memory?

更新:我已将应用转换为ARC。但仍显示内存警告。

1 个答案:

答案 0 :(得分:0)

强烈建议您将(如果可以)转换为ARC。编译器能够为您插入最有效的保留/版本。也可以完全消除对SafeRelease等任何需求。