Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4267244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:52:23+00:00 2026-05-21T06:52:23+00:00

I am pretty new to Objective C, and even brand new to using SQL

  • 0

I am pretty new to Objective C, and even brand new to using SQL inside my apps. I am trying to get a running count of the state of items in my database. If you can imagine a todo type app, with a number of items in each category. The method below first retrieves the total number of items in the category, then loops through each category and determines which ones have the value of “Yes” (as in yes it is checked/completed). The code works, but I have this tied to a UIButton, so when the button is pressed the method below gets called as well as the UIPopover that displays the data. When the UIButton is pressed the first few times, it is very responsive and immediately opens the UIPopover, however when it is pressed many times, is slows down, eventually it takes a second or two to open. I want to know the best way to do this correctly, without hogging memory and keeping everything responsive. I must be doing something completely wrong as it gets more and more sluggish as you press the button to open the popover. Any ideas or suggestions would be really appreciated.

-(void)doCount{

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"ListDatabase.sql"];

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

for (NSString *element in self.countArray){

    NSString *retrieveValue = [prefs objectForKey:@"selectedList"];

        const char *sql = [[NSString stringWithFormat:@"SELECT COUNT(itemCategory)FROM '%@' WHERE itemCategory='%@'",retrieveValue,element]UTF8String];

        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                int totalcount = sqlite3_column_int(selectstmt,0);

                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                [prefs setInteger:totalcount forKey:@"count"];

                }


        const char *sql = [[NSString stringWithFormat:@"SELECT COUNT(itemDone)FROM '%@' WHERE itemDone='Yes' AND itemCategory ='%@'",retrieveValue,element]UTF8String];

        sqlite3_stmt *getcountstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &getcountstmt, NULL) == SQLITE_OK) {

            while(sqlite3_step(getcountstmt) == SQLITE_ROW) {

                int count = sqlite3_column_int(getcountstmt,0);

                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

                int totalcount = [prefs integerForKey:@"count"];


                NSString *catCount = [NSString stringWithFormat: @"%i/%i",count,totalcount];

                [prefs setObject:catCount forKey:element];

            }                            
        }

        sqlite3_finalize(selectstmt);    
        sqlite3_finalize(getcountstmt);


    }

}
}
   sqlite3_close(database);

    //[self getFinishedItems];

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:52:24+00:00Added an answer on May 21, 2026 at 6:52 am

    Greg,
    here are few improvements in the above code…

    -(void)doCount{
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"ListDatabase.sql"];
    
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
    
    for (NSString *element in self.countArray){
    
        NSString *retrieveValue = [prefs objectForKey:@"selectedList"];
    
        NSMutableString * sqlQuery = [[NSMutableString alloc]initWithFormat:@"SELECT COUNT(itemCategory)FROM '%@' WHERE itemCategory='%@'",retrieveValue,element];
    
            const char *sql = [sqlQuery UTF8String];
            [sqlQuery release];
    
            sqlite3_stmt *selectstmt;
    
            if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
    
                while(sqlite3_step(selectstmt) == SQLITE_ROW) {
    
                    int totalcount = sqlite3_column_int(selectstmt,0);
            //comment following line.
                    //NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                    [prefs setInteger:totalcount forKey:@"count"];
    
                    }
    
        sqlQuery =[[NSMutableString alloc]initWithFormat:@"SELECT COUNT(itemDone)FROM '%@' WHERE itemDone='Yes' AND itemCategory ='%@'",retrieveValue,element];
            const char *sql = [sqlQuery UTF8String];
        [sqlQuery release];
    
            sqlite3_stmt *getcountstmt;
            if(sqlite3_prepare_v2(database, sql, -1, &getcountstmt, NULL) == SQLITE_OK) {
    
                while(sqlite3_step(getcountstmt) == SQLITE_ROW) {
    
                    int count = sqlite3_column_int(getcountstmt,0);
    
            //comment following lines...
                    //NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    
                    int totalcount = [prefs integerForKey:@"count"];
    
    
                    NSMutableString *catCount = [[NSMutableString alloc]initWithFormat: @"%i/%i",count,totalcount];
    
                    [prefs setObject:catCount forKey:element];
            [catCount release];
    
                }                            
            }
    
            sqlite3_finalize(selectstmt);    
            sqlite3_finalize(getcountstmt);
    
    
        }
    
    }
    }
       sqlite3_close(database);
    
        //[self getFinishedItems];
    
    }
    

    also, you can put your dbPath creation code somewhere else or say set it up globally so that it won’t create the autoreleased object which also occupy the memory…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still pretty new to Objective -C and iPhone development and am trying to
Im pretty new to objective-c programming and releasing of objects is my greatest headache.
I pretty new to Objective-C (and C itself) and need to consume a NSData
I'm still pretty new to Objective-C coding (as evidenced by this question) and I
I'm pretty new to Objective C and iPhone development and have hit a problem
I'm pretty new to Objective C but things are progressing well. However, I think
I am pretty new to Objective-C (so far I was working with .NET and
As I'm pretty new to Objective-C and memory management, I was curious if this
I'm new to Objective C and have a pretty basic question. So I have
I'm pretty new to Objective-C and I'm working on a webradio app. My app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.