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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-10-2010, 07:01 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: UK, North East
Posts: 227
zardon is on a distinguished road
Default Models for FMDB; and how to stop repeating myself

I'm struggling with Models for database tables in Objective C.

The problem I'm having is that I appear to be repeating myself a lot in code; and I'm not sure if I am doing it right.

I have successfully setup FMDB to pull in a simple database. The database will allow you to add/edit/delete from 2 tables; "Teams" and "Players".

Okay, here's the breakdown.

EditorAppDelegate -->
here I init the FMDatabase, and do a quick test to make sure the database has been loaded; and I do a quick SELECT * FROM teams. Everything is fine.
RootViewController -->
This is just a menu where it says "Teams" and "Players".
TeamViewController -->
This is a TableViewController where it will eventually display the teams.

Now, what I want is to move the SELECT * FROM teams (and other SQL stuff) into its own model. This is considered best practice.
Okay. So I set up a model: TeamsModel.h and TeamsModel.m

I create a method called "getTableContents" where it does the simple SELECT * command from my earlier tests.

Problem 1: FMDB cannot be found

It no longer see's FMDB anymore, even though I included it in my AppDelegate file, and its an AppDelegate variable.

It no longer see's FMResultSet; even though its included the AppDelegate file.

Problem 2: A lot of repetitive coding! UGH!

My current solution to problem 1 is just to include the AppDelegate and all the database files again.

IE:

Code:
#import "EditorAppDelegate.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
And then in my getTableContents method I do:

Code:
-(void)getTableContents
{
	EditorAppDelegate *appDelegate = (EditorAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	FMResultSet *rs = [appDelegate.db executeQuery:@"SELECT * FROM Teams"];
	
    if ([appDelegate.db hadError])
	{
        NSLog(@"Err %d: %@", [appDelegate.db lastErrorCode], [appDelegate.db lastErrorMessage]);
    }
    // some other processing would go here
}
I know this is wrong.

1. I'm including the App Delegate; but I need this because the AppDelegate has the "db", a pointer to FMDatabase.

2. I'm including the FMDatabase files more than once in the whole application!

What annoys me is that if I have another 6 odd tables doing add/edit/delete's for their respective tables I would need to keep including the relevant files over and over again.

I really hate repeating myself, and I know that you should not be including the App Delegate like I have above.

Therefore, I'm looking for the best practices when it comes to Model files for databases and how to reduce the amount of times I need to include files, etc.

If this isn't clear enough, please let me know.

Any help would be great.
zardon is offline   Reply With Quote
Old 09-10-2010, 11:18 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 26
jhockman is on a distinguished road
Default

I'm new to this kind of thing and your database handling is a little over my head, but would it be possible to just create your own header file that has all the necessary #import lines then just #import that one file in your other classes. Then you would just need to update that one header file.

As for your other questions like why you should need to add your app delegate I don't know.
jhockman is offline   Reply With Quote
Old 09-10-2010, 12:50 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: UK, North East
Posts: 227
zardon is on a distinguished road
Default

Quote:
Originally Posted by jhockman View Post
I'm new to this kind of thing and your database handling is a little over my head, but would it be possible to just create your own header file that has all the necessary #import lines then just #import that one file in your other classes. Then you would just need to update that one header file.

As for your other questions like why you should need to add your app delegate I don't know.
Yeah, for the moment I'm using one import file for all the stuff I want to include.

The reason why I'm adding the app delegate is because it has a pointer to the FMDatabase, which is a wrapper for SQLite databases. It seems to work, although I've been told to be cautious with larger databases.

I believe another answer would be to move all this App Delegate variables out into a Singleton class; I'm not too sure what they are but I will read up on them and see if this helps.

I'm sure I'm not meant to be including the App Delegate here, there and everywhere.

In addition, the only way to make Model files to understand what FMDatabase is, is to include it.

It just seems annoying that I have to keep repeating stuff. Other answers would be appreciated.
zardon is offline   Reply With Quote
Reply

Bookmarks

Tags
database, fmdb, models

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
10 members and 319 guests
bignoggins, carlandrews, flamingliquid, hzwegjxg, ilmman, jenniead38, linkmx, nadav@webtview.com, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:51 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0