Performance when building the Objective C application in the device - objective-c

I have a performance problem when I build the application in the Device. It is actually in my database. I have a table of wine details in which there are 2114 wine names. To get the all those wine names, I wrote this code in the appDelegate:
-(NSMutableArray*)getWineDetails
{
[wineDetailsList removeAllObjects];
sqlite3_stmt* statement;
const char *sql = "select *from wineDetails order by name";
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK)
{
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
while (sqlite3_step(statement) == SQLITE_ROW)
{
primaryKey = sqlite3_column_int(statement, 0);
//printf("\n primaryKey1 Value:%d",primaryKey);
wineDetails *wineDets = [[wineDetails alloc] initWithPrimaryKey:primaryKey database:database];
[wineDetailsList addObject:wineDets];
//printf("\n ==========================%d",[wineDetailsList count]);
[wineDets release];
}
sqlite3_finalize(statement);
printf("\n Inside AppDelegate .....wineDetailsList count:%d",[wineDetailsList count]);
return wineDetailsList;
}
I am calling this method in the viewWillAppear of another controller where I have to display the wine names in the table view.
The viewWillAppear code:
-(void)viewWillAppear:(BOOL)animated
{
CorkItAppDelegate* appDelegate = (CorkItAppDelegate*)[[UIApplication sharedApplication] delegate];
winesList = [appDelegate getWineDetails];
[tableView reloadData];
}
Here the problem is that when I build it in the device, it takes too much time to navigate into the controller due to the amount of date in the database. What should I do to get rid of this performance issue?
Thanks,
Monish Kumar.

Just as a quick suggestion, you could add an index on the name column, that might speed up the fetch. Also, make sure you're not fetching any more things than you need.

Related

Objective C: SQLite where-statement wont work when running another method first

So basically I have an app that will provide tasks based on selected project. Both projects and tasks are stored in a SQLite database.
To get the current project id I compare the selected project (_selectedProject) to my database, to get the ID. This is done in my getSelectedProjectId method. However, when running this method in the getTasks method, the Where-statement wont work at all. If I don't run the getSelectedProjectId method first, it works just fine. Am I forgetting to release something? Or is it something else? Any ideas?
I'm pretty new to both SQLite and Objective C, so this may not be a complex issue. I have made sure the getSelectedProjectId method returns the correct project ID. I have also made sure the query that is run in the getTasks method is correct, and when running it through my terminal it returns a number of rows. In the app it returns nothing, provided I'm running the getSelectedProjectId somewhere in that method first.
This is the method that fetches the tasks:
- (void)getTasks
{
[self openDB];
sqlite3_stmt *statement;
int projectId = [self getSelectedProjectId];
NSString *query = [NSString stringWithFormat:#"SELECT * FROM tasks WHERE project_id=%i", projectId];
const char *query_statement = [query UTF8String];
sqlite3_prepare_v2(_contactDB, query_statement, -1, &statement, NULL);
while (sqlite3_step(statement) == SQLITE_ROW)
{
// I add the task title to my array of tasks here.
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
And this is the method that gets the correct project id from the database:
- (int)getSelectedProjectId
{
sqlite3_stmt *statement;
NSString *query = [[NSString alloc]
initWithFormat:#"SELECT id FROM projects WHERE title=\"%#\" LIMIT 0,1",
_selectedProject];
int rowId = 0;
const char *query_statement = [query UTF8String];
[self openDB];
sqlite3_prepare_v2(_contactDB, query_statement, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_ROW)
{
rowId = sqlite3_column_int(statement, 0);
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
return rowId;
}
The problem occured because I closed the DB connection in my getSelectedProjectId-method. I'm now leaving my DB open instead, works like a charm.

Calling two times a function of a static variable?

I have this class containing a static variable "database" which represent a database realized with sqlite and a function getAllShop which task is recalling all the data that are stored in the db and filling them into a mutable array
#define kFilename #"negozi.sqlite"
#implementation ShopDatabase
static ShopDatabase *database;
+(ShopDatabase *)database{
if (database==nil) {
database = [[ShopDatabase alloc] init];
return database;
}
}
- (id)init
{
self = [super init];
if (self) {
// Create the path to the database in the Documents directory for the bundle itself is not writable
NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsToDocuments objectAtIndex:0];
databasePath = [documentsDirectory stringByAppendingPathComponent:kFilename];
if (![[NSFileManager defaultManager] isReadableFileAtPath:databasePath]) {
if ([[NSFileManager defaultManager] copyItemAtPath:yourOriginalDatabasePath toPath:databasePath error:NULL] != YES)
NSAssert2(0, #"Fail to copy database from %# to %#", yourOriginalDatabasePath, databasePath);
}
// --- open db
if(sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK){
NSLog(#"Failed to open database");
}else {
NSLog(#"Database opened");
}
}
return self;
}
- (NSMutableArray *) getAllShops{
// ------ read all the db
NSMutableArray *returnArray=[[NSMutableArray alloc] init];
NSString *query= #"SELECT * FROM negozio";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK){
NSLog(#"Prepared database");
while (sqlite3_step(statement)==SQLITE_ROW) {
int uniqueId = sqlite3_column_int(statement, 0);
NSMutableString *nome = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
ShopInfo *info= [[ShopInfo alloc] initWithUniqueId:uniqueId nome:nome];
[returnArray addObject:info];
}
sqlite3_finalize(statement);
}
return returnArray;
}
#end
When i have to take data from the database from another class i do this, calling the getAllShop and all goes well. In this way i have all the data of the db into my array shopinfo:
NSMutableArray *shopInfo=[[ShopDatabase database] getAllShops];
Now, my database contains data that i need to use to fill TWO table view, so i need to execute this TWO times: one time in the class representing the first table view and one in the second. When i do this in the first view all goes well, but when i do the same the second time, Xcode give me a exc bad access error. I tried executing the code two times in the same class and this is what i get
2012-05-11 13:06:54.897 Shopping Mall[11333:707] -[NegozioPartenza getAllShops]: unrecognized selector sent to instance 0x14b8c0
2012-05-11 13:06:54.899 Shopping Mall[11333:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NegozioPartenza getAllShops]: unrecognized selector sent to instance 0x14b8c0'
*** First throw call stack:
(0x33ad188f 0x325c3259 0x33ad4a9b 0x33ad3915 0x33a2e650 0xa4141 0x35727e33 0x3574c629 0x35710d7d 0x357d34dd 0x3571955d 0x3571940b 0x357d34a3 0x35788873 0x357881b7 0x357d1d89 0x357d04eb 0x3582b82b 0x33a2b3fd 0x35709faf 0x35709f6b 0x35709f49 0x35709cb9 0x3570a5f1 0x35708ad3 0x357084c1 0x356ee83d 0x356ee0e3 0x32fa622b 0x33aa5523 0x33aa54c5 0x33aa4313 0x33a274a5 0x33a2736d 0x32fa5439 0x3571ce7d 0xa2515 0xa24c0)
terminate called throwing an exception(lldb)
I am a newbie to objective C so i can't manage to understand what's the point. How can i call two times a function on a static variable? Thank you.
Edit: maybe calling the [ShopDatabase database] activate a second time the constructor of initializazione making mess? When i say that a variable is static it mean there's only one instance of it for every object of that class, right? So how i can access that unique instance after creating it the first time? I think i'm messing up what happen when you use a static variable...
You don't have a valid return on the second call.
+(ShopDatabase *)database{
if (database==nil) {
database = [[ShopDatabase alloc] init];
return database;
}
}
On the second call database is not nil and you don't return anything. You should be getting a warning that not all control paths return a value.
Here is the proper way.
+(ShopDatabase *)database{
if (database==nil) {
database = [[ShopDatabase alloc] init];
}
return database;
}
Somewhere after your first call, database is being released. Subsequently, an object of type NegozioPartenza is being created at the same location. The second call to [ShopDatabase database] returns this object, and you then send -getAllShops to that object, which obviously doesn't implement that method.

SQLite data showing

I'm totally new with SQLite. I have a UITableview with contains different days in it. (Monday till sunday). When i click on for example Monday an other viewcontroller contains with also a UITableview inside it. In the same viewcontroller i have a UIButton when i click on it i can add data to my SQLite database [A], i insert the name and the day of the week (The day of the week is in this example 'monday' that's because i clicked on the monday view controller).
When i insert a name it appears in my tableview. But when i go back to my first viewcontroller with the days and i click for example on Wednesday the data i added also appear there.
So my question is; How can i show the name which i inserted in monday, only in the monday tableview and not the other days(tableviews)
More information:
So when a user adds a name in 'monday' i send the dayoftheweek with the added name to the SQLite database, when a user adds a name in wednesday i send 'dayoftheweek' Wednesday etc..
Database Coffee looks like =
CoffeeName | dayoftheweek
-------------------------
Hello world | Monday
Hello Planet | Wednesday
Hello Animal | Monday
Hello STOVW | Friday
[A] const char *sql = "insert into Coffee(CoffeeName, dayoftheweek) Values(?, ?)";
I need to check if the day (for example) monday is the same as dayoftheweek (monday) and then display al the items which contains 'dayoftheweek monday'
My sqlite looks like:
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select coffeeID, coffeeName from coffee";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.LessonName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
coffeeObj.dayoftheweek = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
coffeeObj.isDirty = NO;
[appDelegate.coffeeArray addObject:coffeeObj];
}
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
- (void) addCoffee1 {
if(addStmt == nil) {
const char *sql = "insert into Coffee(CoffeeName, dayoftheweek) Values(?, ?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(addStmt, 1, [dayoftheweek UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
LesID = sqlite3_last_insert_rowid(database);
//Reset the add statement.
sqlite3_reset(addStmt);
}
Insert:
coffeeObj.dayoftheweek = [NSString stringWithFormat:#"%#", dayoftheweek];
this insert: monday tuesday wednesday thursday friday saturday or sunday
But how can i display the data which is inserted in monday in the monday tableview and the data which is inserted in tuesday in the tuesday controller etc.
i tried ;
if([coffeeObj.dayoftheweek isEqualToString:#"Monday"]) {
cell.day.text = coffeeObj.LessonName;
} else {
}
Display:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = #"DaycusViewController";
DaycusViewController *cell = (DaycusViewController *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DaycusViewController"
owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[DaycusViewController class]])
cell = (DaycusViewController *)oneObject;
}
//Get the object from the array.
Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];
cell.Name.text = CoffeeObj.CoffeeID;
cell.Day.text = CoffeeObj.dayoftheweek;
//i tried this: (not working)
/* begin */
if([CoffeeObj.dayoftheweek isEqualToString:#"Monday"]) {
// cell.Name.text = CoffeeObj.CoffeeID;
//cell.Day.text = CoffeeObj.dayoftheweek;
} else {
}
/* end */
//it need's to display in this example only things where dayoftheweek is monday but.
return cell;
}
call to function getInitialDataToDisplay
//Copy database to the user's phone if needed.
[self copyDatabaseIfNeeded];
//Initialize the coffee array.
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.coffeeArray = tempArray;
[tempArray release];
//Once the db is copied, get the initial data to display on the screen.
[Coffee getInitialDataToDisplay:[self getDBPath]];
It's hard to understand your question but i think you can better open a new project and start with Core Data. It's easy to understand and it's faster than SQLite.
Core Data is a framework Apple provides to developers that is described as a “schema-driven object graph management and persistence framework.” What does that actually mean? The framework manages where data is stored, how it is stored, data caching, and memory management. It was ported to the iPhone from Mac OS X with the 3.0 iPhone SDK release.
The Core Data API allows developers to create and use a relational database, perform record validation, and perform queries using SQL-less conditions. It essentially allows you to interact with SQLite in Objective-C and not have to worry about connections or managing the database schema
More about Core Data:
https://developer.apple.com/technologies/ios/data-management.html
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html
https://developer.apple.com/library/ios/#referencelibrary/GettingStarted/GettingStartedWithCoreData/_index.html
I wish you all the luck with your application, but i'm for sure that Core Data is the best for your application!
If I understand correctly your problem is not so much about SQLite but much more about how to wire up your view controllers correctly.
When the user selects one of the days in your first UITableViewController you have to pass the information about the selection on to the next view controller. Assuming you are not using storyboards it would probably look something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
DaycusViewController *viewController = [[DaycusViewController alloc] initWithStyle:UITableViewStylePlain];
viewController.selectedDay = [days objectAtIndex:indexPath.row];
[[self navigationController] pushViewController:viewController animated:YES];
[viewController release];
}
As you now have the information available which coffees you want to display (e.g. the ones for "Monday") in your second view controller you can do for example what Diego suggested and filter your data accordingly.
Your current approach does probably not work because the UITableViewDataSource methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
depend on each other. So you have to be consistent here. The first one (...cellForRowAtIndexPath...) will be called for each row you want to display as you indicated in the second one (... numberOfRowsInSection ...).
So if you said you had 4 rows to display it will be called four times. What you cannot do is then use an if statement in the ...cellForRowAtIndexPath ... method to just return less than the four rows. Instead you should have a collection of the coffees for that specific day (ideally coming out of your data model ...). You can use the size of the collection for the ...numberOfRowsInSection... method and then return the corresponding element for each row using the index path in the ...cellForRowAtIndexPath ... method like this:
Coffee c = [coffees objectAtIndex: indexPath.row]
cell.name.text = c.name;
There are some quite good examples in Apple's documentation and some sample code that covers exactly your problem as well.
I'm by no means an expert of objective-c, but could this work?
// Pass dayoftheweek to the function
(void) getInitialDataToDisplay:(NSString *)dbPath:(NSString *)dayoftheweek {
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select coffeeID, coffeeName from coffee where dayoftheweek = ?";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
// Pass dayoftheweek to the query
sqlite3_bind_text(addStmt, 1, [dayoftheweek UTF8String], -1, SQLITE_TRANSIENT);
// Rest of the code, now the query will only return the data for the specified day

Getting number of rows from SQLite C interface in Objective-C

I am new to objective-C and iphone apps.
I am accessing SQLite and have 3 rows in my table "coffee". I used the following way to grab sth out from the table, however, only then 2nd and 3rd rows are being pulled out {the 1st row is always missed}. Is that due to the logic in my while loop by checking while sqlite3_step(selectstmt) returns SQLITE_ROW is wrong? Here is the code:
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select coffeeID, coffeeName from coffee";
sqlite3_stmt *selectstmt;
NSLog(#"sqlite_prepare_v2 returns: %i", sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL));
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
NSLog(#"sqlite3_step returns: %i", sqlite3_step(selectstmt));
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.coffeeName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
NSLog(#"this is the coffee name: %#", coffeeObj.coffeeName);
coffeeObj.isDirty = NO;
[appDelegate.coffeeArray addObject:coffeeObj];
[coffeeObj release];
}
}
}
On the other hand, is there any convenient way for me to check the number of rows returen in a query directly from the C interface of SQLite?
Many thanks.
You could use the query SELECT COUNT(*) FROM coffee to tell you how many rows there are.
And also, save yourself some headaches and use a SQLite wrapper.
Are the 2 sqlite3_step() calls meant to be executed here?
NSLog(#"sqlite3_step returns: %i", sqlite3_step(selectstmt));
while(sqlite3_step(selectstmt) == SQLITE_ROW {
BTW: there a parenthesis missing in the while line. Do not rewrite your code for SO. Copy/Paste it to avoid copying errors (pasting errors are much more rare)

Using pointers in Objective C for NSMutableArray objects

When retrieving objects from an NSMutableArray in cocoa-touch is the below code ok? Should I be allocating([alloc]) new Page objects each time or is just pointing to it alright? Do I need to do anything to the Page *pageObj after, such as set it to nil?
const char *sql = "insert into Page(Book_ID, Page_Num, Page_Text) Values(?, ?, ?)";
for (i = 0; i < ([[self pagesArray] count] - 1); i++) {
if(addStmt == nil) {
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
}
Page *pageObj = [[self pagesArray] objectAtIndex:i];
if(pageObj.isNew) {
sqlite3_bind_int(addStmt, 1, self.book_ID);
sqlite3_bind_int(addStmt, 2, pageObj.page_Number);
sqlite3_bind_text(addStmt, 3, [[pageObj page_Text] UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt)) {
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
}
NSLog(#"Inserted Page: %i into DB. Page text: %#", pageObj.page_Number, pageObj.page_Text);
}
//Reset the add statement.
sqlite3_reset(addStmt);
}
Thanks. I also understand this should probably be in a transaction but I didn't quite get that working just yet.
The way you're declaring a pointer is correct. You don't need alloc, since that creates a new object, when you want to refer to an existing object in the array. You would want to retain it if you were going to keep the reference outside of that method, but since you're only using it temporarily it's fine not to.
The actual pointer variable will be destroyed and recreated every trip to the loop, so there's no need to set it to nil. Even if you declared the variable outside the loop, simply assigning it to a new object is fine. The only time you'd set it to nil is when you're releasing the object stored in the pointer (or the object may be released elsewhere). If you didn't set it to nil in that case, the pointer would refer to an invalid memory location after the object is dealloced, usually causing a crash.
One bug I can see though, you're skipping the last element in your array in your for loop by subtracting 1 from the count.
Aside from the previously mentioned count error, it looks good.
As far as transactions go, I highly recommend wrapping this write loop in one. It will greatly increase your write performance and I found that it helps with memory usage as well. I use the following class method to begin a transaction:
+ (BOOL)beginTransactionWithDatabase:(sqlite3 *)database;
{
const char *sql1 = "BEGIN EXCLUSIVE TRANSACTION";
sqlite3_stmt *begin_statement;
if (sqlite3_prepare_v2(database, sql1, -1, &begin_statement, NULL) != SQLITE_OK)
{
return NO;
}
if (sqlite3_step(begin_statement) != SQLITE_DONE)
{
return NO;
}
sqlite3_finalize(begin_statement);
return YES;
}
and this one to end a transaction:
+ (BOOL)endTransactionWithDatabase:(sqlite3 *)database;
{
const char *sql2 = "COMMIT TRANSACTION";
sqlite3_stmt *commit_statement;
if (sqlite3_prepare_v2(database, sql2, -1, &commit_statement, NULL) != SQLITE_OK)
{
return NO;
}
if (sqlite3_step(commit_statement) != SQLITE_DONE)
{
return NO;
}
sqlite3_finalize(commit_statement);
return YES;
}
I should probably store the SQL statements for later reuse, but these transaction statements are called much less frequently than my other queries.
Of course not. You have allocated it before and are just referencing the same object. No need to reallocate. Also you don't need to set it to nil.