Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 09-02-2010, 01:16 AM   #21 (permalink)
deepak.hws
Deepak Sahu
 
Join Date: Mar 2010
Posts: 1
deepak.hws is on a distinguished road
Send a message via Skype™ to deepak.hws
Default Problem With Sqlite

In this code you have use
+ (void) getInitialDataToDisplayNSString *)dbPath for fetching database.

here sqlite fetching data sequentially in row by row, it take much time when data record's more then (approx 100). So application response is very low.

Can you suggest how to increase application response time?

Thanks
Deepak

Quote:
Originally Posted by ManWithMask View Post
Hi Dean

I am new to iPhone SDK.

I have a simple set of tables which all store one record (e.g. personal details of the iPhone user). So using Arrays does not really make sense for me, since the relationships are all one-to-one.

I would really appreciate some guidance as there is really zip information I can find on anyone doing this...simply reading data straight from the database to to the view objects. Maybe it can't be done on the iPhone and so I am dealing with old programming habits.

Here is my code...hope it helps:


Code:
//
//  IfaAppDelegate.h
//  Ifa
//
//  Created by Marc Tison on 2010/03/21.
//  Copyright Marblesharp (Pty) Ltd 2010. All rights reserved.
//

#import "MyDetailsDatamodel.h"	

@class IfaViewController;

@class MyDetailsDatamodel;

@interface IfaAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    IfaViewController *viewController;
	
	//Creat array to hold My Details data item objects
	NSMutableArray *mydetailsArray;
		
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet IfaViewController *viewController;
@property (nonatomic, retain) NSMutableArray *mydetailsArray;

- (void) copyDatabaseIfNeeded;
- (NSString *) getDBPath;

@end
//
//  IfaAppDelegate.m
//  Ifa
//
//  Created by Marc Tison on 2010/03/21.
//  Copyright Marblesharp (Pty) Ltd 2010. All rights reserved.
//

#import "IfaViewController.h"

@implementation IfaAppDelegate

@synthesize window, viewController;
@synthesize mydetailsArray;

- (void)applicationDidFinishLaunching:(UIApplication *)application {  
	
	//Copy database to the user's phone if needed.
	[self copyDatabaseIfNeeded];
	
	//Initialize the My Details array.
	NSMutableArray *tempArray = [[NSMutableArray alloc] init];
	self.mydetailsArray = tempArray;
	[tempArray release];
	
	//Once the db is copied, get the initial data to display on the screen.
	[MyDetailsDatamodel getInitialDataToDisplay:[self getDBPath]];

	
	// Configure and show the window
	[window addSubview:viewController.view];  
    [window makeKeyAndVisible];
	
}
- (void)applicationWillTerminate:(UIApplication *)application {
	
	// Save data if appropriate
	[MyDetailsDatamodel finalizeStatements];
}
//we get the NSFileManager object to make a copy of the database on the phone
- (void) copyDatabaseIfNeeded {
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSError *error;
	NSString *dbPath = [self getDBPath];
	BOOL success = [fileManager fileExistsAtPath:dbPath];
	
	if(!success) {
		
		NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"iFAdvisor.sql"];
		success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
		
		if (!success)
			NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
	}
}
//Gives us the database location on the user's phone
- (NSString *) getDBPath {
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
	NSString *documentsDir = [paths objectAtIndex:0];
	return [documentsDir stringByAppendingPathComponent:@"iFAdvisor.sql"];
}
//Clear memory
- (void)dealloc {
	[mydetailsArray release];
	[viewController release];
	[window release];
	[super dealloc];
}
@end
//
//  MyDetails.h
//  View
//
//  Created by marcti on 2010/03/17.
//  Copyright 2010 Marblesharp (Pty) Ltd. All rights reserved.
//

#import <sqlite3.h>

@interface MyDetailsDatamodel : NSObject {
	
	//Declare database fields
	NSInteger PKMyDetails;
	NSString *Name;
	NSString *Surname;
	NSString *DateOfBirth;
	NSString *Gender;
	NSString *IdNumber;
	NSString *CellNumber;
	NSString *EmailAddress;
	
	//Internal variables to keep track of the state of the object.
	BOOL isDirty;
	BOOL isDetailViewHydrated;
	
}

@property (nonatomic, readonly) NSInteger PKMyDetails;
@property (nonatomic, retain) NSString *Name;
@property (nonatomic, retain) NSString *Surname;
@property (nonatomic, retain) NSString *DateOfBirth;
@property (nonatomic, retain) NSString *Gender;
@property (nonatomic, retain) NSString *IdNumber;
@property (nonatomic, retain) NSString *CellNumber;
@property (nonatomic, retain) NSString *EmailAddress;

@property (nonatomic, readwrite) BOOL isDirty;
@property (nonatomic, readwrite) BOOL isDetailViewHydrated;

//Static methods.
+ (void) getInitialDataToDisplay:(NSString *)dbPath;
+ (void) finalizeStatements;

//Instance methods.
- (id) initWithPrimaryKey:(NSInteger)pk;

@end
//
//  MyDetails.m
//  View
//
//  Created by marcti on 2010/03/17.
//  Copyright 2010 Marblesharp (Pty) Ltd. All rights reserved.
//

#import "MyDetailsDatamodel.h"

static sqlite3 *database = nil;

@implementation MyDetailsDatamodel

@synthesize PKMyDetails, Name,Surname,DateOfBirth,Gender,IdNumber,CellNumber,EmailAddress;
@synthesize isDirty,isDetailViewHydrated;

+ (void) getInitialDataToDisplay:(NSString *)dbPath {
	
	IfaAppDelegate *appDelegate = (IfaAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
		
		const char *sql = "select PKMyDetails, Name, Surname, IdNumber, Gender,DateOfBirth,CellNumber,EmailAddress from MyDetails";
		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);
				MyDetailsDatamodel *mydetailsObj = [[MyDetailsDatamodel alloc] initWithPrimaryKey:primaryKey];
				mydetailsObj.Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
				mydetailsObj.Surname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
				mydetailsObj.IdNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
				mydetailsObj.Gender = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
				mydetailsObj.DateOfBirth = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
				mydetailsObj.CellNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
				mydetailsObj.EmailAddress = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];
				
				mydetailsObj.isDirty = NO;
				
				[appDelegate.mydetailsArray addObject:mydetailsObj];
				
				[mydetailsObj release];
			}
		}
	}
	else
		sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
+ (void) finalizeStatements {
	
	if(database) sqlite3_close(database);
}

//Initialise the primary key
- (id) initWithPrimaryKey:(NSInteger) pk {
	
	[super init];
	PKMyDetails = pk;
	
	isDetailViewHydrated = NO;
	
	return self;
}

- (void)dealloc {
	[Name release];
	[Surname release];
	[IdNumber release];
	[Gender release];
	[DateOfBirth release];
	[CellNumber release];
	[EmailAddress release];
    [super dealloc];
}

@end
//  MyDetailsController.m
//  iFA
//
//  Created by marcti on 2010/03/10.
//  Copyright 2010 Marblesharp 147 (Pty) Ltd. All rights reserved.
//

#import "MyDetailsController.h"
#import "MyDetailsDatamodel.h"

@implementation MyDetailsController

@synthesize myhealthController, theScroller;
@synthesize NameField, SurnameField, IdNumberField, CellNumberField,EmailAddressField;
@synthesize soundFileURLRef, soundFileObject;

// Implement viewDidLoad to do additional setup after loading the view
- (void) viewDidLoad {
	
	[super viewDidLoad];
	
	theScroller.contentSize=CGSizeMake(280, 650);
	
	// Get the main bundle for the app
	CFBundleRef mainBundle;
	mainBundle = CFBundleGetMainBundle ();
	
	// Get the URL to the sound file to play
	soundFileURLRef  =	CFBundleCopyResourceURL (mainBundle,CFSTR ("tap"),CFSTR ("aif"),NULL);
	
	// Create a system sound object representing the sound file
	AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
	
	//Using the appDelegate we can access the mydetailsArray
	appDelegate = (IfaAppDelegate *)[[UIApplication sharedApplication] delegate];

	//return appDelegate.mydetailsArray.count;
	if(appDelegate.mydetailsArray != nil){
	//	NSLog(@"My Details Array:%@",appDelegate.mydetailsArray);
		//**** we get here, but myDetailsArray is undeclared?
		//NameField.text = mydetailsArray.Name;
		//SurnameField.text = mydetailsArray.Surname;
		//IdNumberField.text = mydetailsArray.IdNumber;
		//CellNumberField.text = mydetailsArray.CellNumber;
		//EmailAddressField.text = mydetailsArray.EmailAddress;
	}		
	
}	

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}
- (void)dealloc {
	[theScroller release];
	[myhealthController release];
	//	[mydetailsDatamodel release];
	[super dealloc];
}
@end
deepak.hws is offline   Reply With Quote
 

» Advertisements
» Online Users: 374
14 members and 360 guests
Absentia, antonwilliams, breennorah, Domele, dre, HemiMG, heshiming, ipodphone, KaliJen00, linkmx, Matl43Owa, mjnafjke, raymng, sathiamoorthy
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,579
Threads: 94,082
Posts: 402,769
Top Poster: BrianSlick (7,990)
Welcome to our newest member, breennorah
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:36 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.