当objective-c中的内存使用量变大时,Singleton类属性变为null

时间:2014-08-27 13:44:22

标签: ios objective-c iphone sqlite singleton

在我的应用程序中,我使用singleton类和sqlite db

这是我的单身人士课程:

#import <Foundation/Foundation.h>

@interface LoggedinUser : NSObject
{
   // NSMutableArray *questionIDs;
}
+ (LoggedinUser *)sharedCenter;
@property (nonatomic,strong) NSString *email;
@property(nonatomic,strong)NSString *firmID;
@property (nonatomic,strong) NSString *name;
@property(nonatomic,strong)NSString *surname;
@property (nonatomic,strong) NSString *userTypeID;
@property(nonatomic,strong)NSString *firmName;
@property(nonatomic,strong)NSString *userID;
@property(nonatomic,strong)NSString *dbFileName;
@property(nonatomic,strong)NSString *answerdbFileName;
@property(nonatomic,strong)NSString *lastOrderIndex;
@property(nonatomic,strong)NSMutableArray *questionIDs;
@property(nonatomic,strong)NSString *programID;
@property(nonatomic,strong)NSString *merchantID;
@property(nonatomic,strong)UIViewController *myQuestionView;
@end

#import "LoggedinUser.h"

@implementation LoggedinUser
@synthesize     userID,surname,name,userTypeID,email,firmID,firmName,dbFileName,answerdbFileName,lastOrderIndex,programID,questionIDs,merchantID,myQuestionView;

static LoggedinUser *sharedLoggedinUser = nil;    // static instance variable

+ (LoggedinUser *)sharedCenter {

static LoggedinUser *sharedInstance = nil;
if (!sharedInstance) {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[super allocWithZone:NULL] init];
        // custom initialization
    });
}
return sharedInstance;

}

- (id)init {
if ( (self = [super init]) ) {
    questionIDs = [[NSMutableArray alloc] init];
    myQuestionView=[[UIViewController alloc] init];
}
return self;
}

+ (id)allocWithZone:(NSZone *)zone {
return [self sharedCenter];
}

- (id)copyWithZone:(NSZone *)zone {
return self;
}

-(void)dealloc {
//[super dealloc];
}


@end

和我的DBHelper对象:

#import "DBHelper.h"
#import "Merchant.h"
#import "QuestionChoices.h"
#import "Question.h"
#import "LoggedinUser.h"
static DBHelper *sharedInstance = nil;
static sqlite3 *database = nil;
static sqlite3_stmt *statement = nil;

@implementation DBHelper

+(DBHelper*)getSharedInstance:(NSString*)dbname{
if (!sharedInstance) {
    sharedInstance = [[super allocWithZone:NULL]init];
    [sharedInstance createDB:dbname];
}
return sharedInstance;
}

-(BOOL)createDB:(NSString *)dbname{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
                [docsDir stringByAppendingPathComponent: dbname]];
return true;
}

- (NSMutableArray*) getRegions
{

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{

    NSString *querySQL = [NSString stringWithFormat:@"select ID,NAME from T_REGION"];
    const char *query_stmt = [querySQL UTF8String];
    NSMutableArray *regionNames = [[NSMutableArray alloc]init];
    NSMutableArray *regionIDs = [[NSMutableArray alloc]init];
    NSMutableDictionary *firstOptionDic =[[NSMutableDictionary alloc] init];

    NSMutableArray *mainArray = [[NSMutableArray alloc]init];

    if (sqlite3_prepare_v2(database,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        [firstOptionDic setValue:@"Seçiniz" forKeyPath:@"name"];
        [firstOptionDic setValue:@"0" forKeyPath:@"ids"];
        [mainArray addObject:firstOptionDic];

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *ids = [[NSString alloc] initWithUTF8String:
                              (const char *) sqlite3_column_text(statement, 0)];
            [regionIDs addObject:ids];

            NSString *name = [[NSString alloc] initWithUTF8String:
                              (const char *) sqlite3_column_text(statement, 1)];
            [regionNames addObject:name];



        }
        sqlite3_finalize(statement);
        //sqlite3_close(database);

        for (int index = 0; index<regionNames.count; index++) {
            NSMutableDictionary *dataAddDic =[[NSMutableDictionary alloc]init];
            [dataAddDic setValue:[regionNames objectAtIndex:index] forKeyPath:@"name"];
            [dataAddDic setValue:[regionIDs objectAtIndex:index] forKeyPath:@"ids"];
            [mainArray addObject:dataAddDic];
        }


        return mainArray;
    }
    else
    {
        NSLog(@"problem");
    }
}
return nil;
}

- (NSMutableArray*) getCityByRegionID:(NSString *) regionID
{

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{

    NSString *querySQL = [NSString stringWithFormat:@"select ID,NAME from T_CITY where REGION_ID=%@ order by ORDER_NO",regionID];
    const char *query_stmt = [querySQL UTF8String];
    NSMutableArray *cityNames = [[NSMutableArray alloc]init];
    NSMutableArray *cityIDs = [[NSMutableArray alloc]init];
    NSMutableArray *mainArray = [[NSMutableArray alloc]init];
    NSMutableDictionary *firstOptionDic =[[NSMutableDictionary alloc] init];
    if (sqlite3_prepare_v2(database,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        [firstOptionDic setValue:@"Seçiniz" forKeyPath:@"name"];
        [firstOptionDic setValue:@"0" forKeyPath:@"ids"];
        [mainArray addObject:firstOptionDic];
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *ids = [[NSString alloc] initWithUTF8String:
                             (const char *) sqlite3_column_text(statement, 0)];
            [cityIDs addObject:ids];

            NSString *name = [[NSString alloc] initWithUTF8String:
                              (const char *) sqlite3_column_text(statement, 1)];
            [cityNames addObject:name];



        }
        sqlite3_finalize(statement);
        //sqlite3_close(database);

        for (int index = 0; index<cityNames.count; index++) {
            NSMutableDictionary *dataAddDic =[[NSMutableDictionary alloc]init];
            [dataAddDic setValue:[cityNames objectAtIndex:index] forKeyPath:@"name"];
            [dataAddDic setValue:[cityIDs objectAtIndex:index] forKeyPath:@"ids"];
            [mainArray addObject:dataAddDic];
        }


        return mainArray;
    }
    else
    {
        NSLog(@"problem");
    }
}
return nil;
}


- (NSMutableArray*) getDistrictByCityID:(NSString *) cityID
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{

    NSString *querySQL = [NSString stringWithFormat:@"select ID,NAME from T_DISTIRICT where CITY_ID=%@ order by NAME",cityID];
    const char *query_stmt = [querySQL UTF8String];
    NSMutableArray *districtNames = [[NSMutableArray alloc]init];
    NSMutableArray *districtIDs = [[NSMutableArray alloc]init];
    NSMutableArray *mainArray = [[NSMutableArray alloc]init];
    NSMutableDictionary *firstOptionDic =[[NSMutableDictionary alloc] init];
    if (sqlite3_prepare_v2(database,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        [firstOptionDic setValue:@"Seçiniz" forKeyPath:@"name"];
        [firstOptionDic setValue:@"0" forKeyPath:@"ids"];
        [mainArray addObject:firstOptionDic];

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *ids = [[NSString alloc] initWithUTF8String:
                             (const char *) sqlite3_column_text(statement, 0)];
            [districtIDs addObject:ids];

            NSString *name = [[NSString alloc] initWithUTF8String:
                              (const char *) sqlite3_column_text(statement, 1)];
            [districtNames addObject:name];



        }
        sqlite3_finalize(statement);
        //sqlite3_close(database);

        for (int index = 0; index<districtNames.count; index++) {
            NSMutableDictionary *dataAddDic =[[NSMutableDictionary alloc]init];
            [dataAddDic setValue:[districtNames objectAtIndex:index] forKeyPath:@"name"];
            [dataAddDic setValue:[districtIDs objectAtIndex:index] forKeyPath:@"ids"];
            [mainArray addObject:dataAddDic];
        }


        return mainArray;
    }
    else
    {
        NSLog(@"problem");
    }
}
return nil;
}

-(int)getMerchantByMerchantName:(NSString *)firmID : (NSString *) name
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{

    NSString *querySQL = [NSString stringWithFormat:@"select ID from T_MERCHANT where NAME like '%%%@' and FIRM_ID=%@ order by NAME",name,firmID];
    const char *query_stmt = [querySQL UTF8String];

    NSMutableArray *merchantIDs = [[NSMutableArray alloc]init];



    if (sqlite3_prepare_v2(database,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *ids = [[NSString alloc] initWithUTF8String:
                             (const char *) sqlite3_column_text(statement, 0)];
            [merchantIDs addObject:ids];




        }
        sqlite3_finalize(statement);
        //sqlite3_close(database);
        if(merchantIDs !=nil)
        {

            if(merchantIDs.count>0) {

                return [[merchantIDs objectAtIndex:0] intValue];

            }
            else
            {

                return 0;
            }
        }
        else
        {
            NSLog(@"problem");
            return 0;
        }

    }
    else
    {
        NSLog(@"problem");
    }
}
return 0;
}

- (NSMutableArray*) getMerchantByFirmID:(NSString *)firmID
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{

    NSString *querySQL = [NSString stringWithFormat:@"select ID,NAME from T_MERCHANT where FIRM_ID=%@ order by NAME",firmID];
    const char *query_stmt = [querySQL UTF8String];
    NSMutableArray *merchantNames = [[NSMutableArray alloc]init];
    NSMutableArray *merchantIDs = [[NSMutableArray alloc]init];
    NSMutableArray *mainArray = [[NSMutableArray alloc]init];
    NSMutableDictionary *firstOptionDic =[[NSMutableDictionary alloc] init];

    if (sqlite3_prepare_v2(database,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        [firstOptionDic setValue:@"Seçiniz" forKeyPath:@"name"];
        [firstOptionDic setValue:@"0" forKeyPath:@"ids"];
        [mainArray addObject:firstOptionDic];

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *ids = [[NSString alloc] initWithUTF8String:
                             (const char *) sqlite3_column_text(statement, 0)];
            [merchantIDs addObject:ids];

            NSString *name = [[NSString alloc] initWithUTF8String:
                              (const char *) sqlite3_column_text(statement, 1)];
            [merchantNames addObject:name];



        }
        sqlite3_finalize(statement);
        //sqlite3_close(database);
        if(merchantNames !=nil)
        {

            for (int index = 0; index<merchantNames.count; index++) {

                NSMutableDictionary *dataAddDic =[[NSMutableDictionary alloc]init];
                [dataAddDic setValue:[merchantNames objectAtIndex:index] forKeyPath:@"name"];
                [dataAddDic setValue:[merchantIDs objectAtIndex:index] forKeyPath:@"ids"];
                [mainArray addObject:dataAddDic];
            }
            if(mainArray.count>0)
            {

                return mainArray;
            }
            else
            {

                return nil;
            }
        }
        else
        {
            NSLog(@"problem");
            return nil;
        }

    }
    else
    {
        NSLog(@"problem");
    }
}
return nil;
}

这就是我调用数据的方式:

NSArray *countries = [[NSArray alloc] initWithArray:[[DBHelper getSharedInstance:[LoggedinUser sharedCenter].dbFileName] getMerchantByFirmID:[LoggedinUser sharedCenter].firmID]];

现在我的问题是,当长时间使用应用程序时,应用程序内存使用量达到140-150mb然后崩溃。当我调试代码时LoggedinUser的dbfilename变为null但在应用程序中没有任何地方我从未将其设置为null。我该怎么做才能改善我的内存管理,或者使用单例类或sqlite数据库使用有什么技巧吗?

0 个答案:

没有答案