Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-04-2007, 04:27 AM   #1
DrGoomba
Registered User
 
Join Date: Jan 2007
Posts: 30
Default Problem accessing property of Singleton Class

First off, i am really confused on how to make properties global in a flex app and this is probably why i can't grasp this concept. But i have created a singleton class to instantiate a database connection. When i try to access the connection through another view AIR throws me an error saying "database must be open to perform this operation". But i did open it at the begining of the app, and in my singleton, if it hasn't been already initialized, then it will initialize it. But this tells me the class knows its already open otherwise i wouldn't get that error. So i am obviously not accessing it properly. What am i missing? How do i access my static property in other components after it has been inistantiated?

Here is my singleton class
ActionScript Code:
package com.smallfry.connections.db {         import flash.data.SQLConnection;     import flash.filesystem.File;     import flash.events.SQLEvent;     import flash.events.SQLErrorEvent;         public class DatabaseConnector extends SQLConnection {                 private static var _instance:DatabaseConnector;                 public function DatabaseConnector(enforcer:SingletonEnforcer) {             var conn:SQLConnection = new SQLConnection();             conn.addEventListener(SQLEvent.OPEN, openHandler);             conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);             var dbFile:File = File.applicationStorageDirectory.resolve("mydatabase.sqlite");                     conn.open(dbFile, false);         }                 public static function getInstance():DatabaseConnector {             if (DatabaseConnector._instance == null) {                 DatabaseConnector._instance = new DatabaseConnector(new SingletonEnforcer());             }             return DatabaseConnector._instance;         }                 private function openHandler(event:SQLEvent):void {             trace("the database initialized successfully");         }                 private function errorHandler(event:SQLErrorEvent):void {             trace("Error code:", event.error.code);             trace("Details:", event.error.message);         }             } } class SingletonEnforcer {}

My main application:
ActionScript Code:
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:views="views.*">             <mx:Script>         <![CDATA[             import com.smallfry.connections.db.DatabaseConnector;                         private var conn:DatabaseConnector = DatabaseConnector.getInstance();                     ]]>     </mx:Script>            <mx:TabNavigator y="10" width="818" height="700" x="10">         <views:Inventory/>         <views:InventoryRec  verticalScrollPolicy="off" horizontalScrollPolicy="off" width="100%" height="100%"/>     </mx:TabNavigator>     </mx:WindowedApplication>


Then the InventoryReceiving.mxml:
ActionScript Code:
private var selectStmt:SQLStatement = new SQLStatement(); private var conn:DatabaseConnector = DatabaseConnector.getInstance();             private function genLocatorCode(spaceRequired:Number):void {         // create the SQL statement     selectStmt.sqlConnection = conn;                         // define the sql text     var sql:String =         "SELECT min(locCode) " +         "FROM Location ";                             selectStmt.text = sql;                         // register listeners for the result and error events     selectStmt.addEventListener(SQLEvent.RESULT, selectResult);         selectStmt.addEventListener(SQLErrorEvent.ERROR, selectError);                         // execute the statement     selectStmt.execute(); }
DrGoomba is offline   Reply With Quote
Old 07-04-2007, 12:46 PM   #2
hangalot
lala
 
hangalot's Avatar
 
Join Date: Feb 2002
Location: on the road
Posts: 2,858
Default

what version of AIR?
__________________
oi poloi
http://www.memorphic.com/news/
hangalot is offline   Reply With Quote
Old 07-04-2007, 10:22 PM   #3
DrGoomba
Registered User
 
Join Date: Jan 2007
Posts: 30
Default

Good question. I dont even know how to find that out. But i did download it less than a week ago, so it should be the current version. I did get it to work correctly though by adding a getter which i could reference.

The modified part of the class looks like this:
ActionScript Code:
private static var _instance:DatabaseConnector; private var _conn:SQLConnection;         public function get database():SQLConnection {     return _conn; }         public function DatabaseConnector(enforcer:SingletonEnforcer) {     trace("i have initialized");     var conn:SQLConnection = new SQLConnection();     conn.addEventListener(SQLEvent.OPEN, openHandler);     conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);     var dbFile:File = File.applicationStorageDirectory.resolve("mydatabase.sqlite");     conn.open(dbFile, false);     _conn = conn; }


And now i reference by calling the database property DatabaseConnector.getInstance().database

If there is a more efficient way of doing this, please don't hold back.

Another thing. The database i am accessing i created using SQLiteManager but AIR doesn't seem to like it. I don't get any errors connecting to it, but i presume this is because my code creates a new database if one doesn't already exist. So this would explain when i try to access a table in the generated db that i receive an error saying the table cannot be found.

I would like to be able to include a separate database with my app, just so its easier to for people to make backups or be able to access it through other applications. Any ideas on why i am unable to use a pre-defined database?

Thanks!!!
DrGoomba is offline   Reply With Quote
Old 07-06-2007, 12:15 PM   #4
hangalot
lala
 
hangalot's Avatar
 
Join Date: Feb 2002
Location: on the road
Posts: 2,858
Default

i have not used the sqlite yet, i asked some people i work with about their experience with it but they could not say for certain.
i think a singleton is a very valid way of doing this
__________________
oi poloi
http://www.memorphic.com/news/
hangalot is offline   Reply With Quote
Old 07-06-2007, 04:46 PM   #5
DrGoomba
Registered User
 
Join Date: Jan 2007
Posts: 30
Default

I've figured it out. It didnt like my pre built database because i was trying to access it via the applicationStorageDirectory rather than a user file directory such as desktopDirectory. now it works.
DrGoomba is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Flash Remoting with Perl dopefries Flash Remoting 3 02-17-2007 02:42 PM
crossdomain globals... big problem with class static variables ! _marabout ActionScript 2.0 2 07-21-2006 03:07 PM
scope problem in a class hbd ActionScript 2.0 4 08-31-2005 02:10 PM
Skinning components with embedded components epeace Components 5 06-16-2005 09:35 PM
problem accesing instance properties defined in a class glantucan ActionScript 2.0 3 05-06-2005 02:54 PM


All times are GMT. The time now is 04:26 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.