iphone - ARC 中的内存问题

标签 iphone ios objective-c sqlite

我使用 UINavigationcontroller 作为 Root View Controller 。我是iphone ARC 和ARC 下的sqlite 操作的新手。

  1. 我的 rootviewcontroller 是 loginviewcontroller 接下来是 homeview,它包含带有 4 个 UIviewcontroller 的 scrollView。
  2. 我正在使用 Instruments 进行测试,因为我收到内存警告。使用我的应用程序大约 5-10 分钟后,当我注销(为此我使用 popToRootViewController)并再次登录时,Live Bytes 继续增加,当我检查 Call Trees -

     [NSObject(NSTreadPerformAdditions) performselectorOnMainThread:withObject:waituntilDone:] 
    

显示 15.91MB Bytes 已使用,随后显示

我也在做 SQLite 操作,在执行这些操作时,它添加了

if(sqlite3_open([[self filePath]UTF8String],&dBObject)!= SQLITE_OK)

显示 10MB Live Bytes 用于打开数据库、selectquery 和更新

我第一次使用 Instruments 测试我的应用程序时,它显示了以下内容: enter image description here

我现在如何释放这段内存?

编辑:

我正在使用这段代码来检索数据

-(int)CheckCompanyIsAvailableInTradeList:(NSString *)CompanyId UserId:(NSString *)userId
{
int check=0;
if(sqlite3_open([[self filePath] UTF8String], &dBObject) == SQLITE_OK) {
    NSString *sqlStatement =NULL;
    sqlStatement = [ NSString stringWithFormat:@"SELECT COUNT(*) FROM tradelist WHERE compnyID = '%@' and UserMasterID ='%@' ;",CompanyId,userId];

    if(sqlite3_prepare_v2(dBObject, [sqlStatement UTF8String], -1, &statement, NULL) == SQLITE_OK) {
        @autoreleasepool {
            while(sqlite3_step(statement) == SQLITE_ROW) {
                check = sqlite3_column_int(statement, 0);

            }
        }



    }
    sqlite3_finalize(statement);

}
sqlite3_close(dBObject);
return check;

}

whenever this method is called live bytes goes on increasing continously

最佳答案

试试这个,

在开始循环之前,您必须打开数据库...

完成后你必须关闭你的数据库..

例如

 -(void)downloadData
 {
 [self  openDB];// database is open..

 // start your loop 
for (int i=0; i<[array count]; i++)
{
   [self executeInsertQuery];

}

[self  closeDb]; 
 }


-(void)openDB
{
 if(sqlite3_open([[self filePath]UTF8String],&dBObject)!= SQLITE_OK)
{
    sqlite3_close(dBObject);
}

} 


-(void)closeDb
{
if (dBObject)
{

    int rc = sqlite3_close(dBObject);
    NSLog(@"close rc=%d", rc);

    if (rc == SQLITE_BUSY)
    {

        sqlite3_stmt *stmt;
        while ((stmt = sqlite3_next_stmt(dBObject, 0x00)) != 0)
        {
            NSLog(@"finalizing stmt");
            sqlite3_finalize(stmt);
        }

        rc = sqlite3_close(dBObject);
    }

    if (rc != SQLITE_OK)
    {
        NSLog(@"close not OK.  rc=%d", rc);
    }

    dBObject = NULL;
}

}

  -(Bool)executeInsertQuery
{


  NSString* strQuery=[NSString stringWithFormat: @"Insert into   list(compnyID,UserMasterID) values('%@','%@');",companyId ,userId ];

 char *err;
if (sqlite3_exec(dBObject, [queryString UTF8String], NULL,NULL, &err)!= SQLITE_OK)
{
    sqlite3_close(dBObject);
    return NO;
}
else
{
    return YES;
}


}

关于iphone - ARC 中的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16856328/

相关文章:

ios - 每行结果相同

iphone - dispatch_async 会降低 UI 和应用程序的速度

ios - 我无法加载存储在我的文档目录中的数据

objective-c - 调用时 UITableView 部分不重新加载

ios - iOS8 beta 5 上的 UIWebView 中的定位服务无法正常工作

ios - UIP 英寸手势 ?为什么只有在旋转设备后才显示缩放结果

ios - 如何从另一个 iOS 应用程序打开谷歌街景应用程序

iphone - UIView重叠

iphone - 如果我们传递 NSData 如何播放音频?

iphone - 如何确保应用程序已正确签名