Sep 11, 2008

(Android) How to resolve the exception of "Table has not been deactivated or closed"

I'm working on the migration my project from M5 to 0.9, i got some exceptions of SQLite which shows "Table/Cursor has not been deactivated or closed" in my database class, i found the problems come from the Cursor. After read the API documents, i understand each opened Cursor should be deactivated and closed after reading its content. Here is an example:

Cursor cursor = sqlite.query(table, null, selection, null, null, null, null);
if (cursor.getCount() > 0){
int cursorCount = cursor.getCount();
ArrayList records = new ArrayList();
cursor.moveToFirst();
for (int i=0; i record = new HashMap();
String[] columns = cursor.getColumnNames();
int columnsNb = columns.length;
for (int column=0; column

(Objective-C) How to block thread

In the multi-threading programming, sometimes we need block the other threads to wait the result of a thread. There are several solutions:

1. Using @synchronized. With this solution, we don't create a NSThread object, here is an example:

- (void) callThreadMethod {
@synchronized(self) {
//the methods you want the thread to do
}
}

2. Using NSLock. Still don't create NSThread object, here is an example:

- (void) callThreadMethod {
NSLock *myLock = [[NSLock alloc] init];
[myLock lock];
//the methods you want the thread to do
[myLock unlock];
}

For the other solutions, you could read this great tutorial of multi-threading, Multithreading Tutorial

Sep 8, 2008

(iPhone) How to use Preference

Writing a simple data to preference:

CFStringRef textColorKey = CFSTR("defaultTextColor");
CFStringRef colorBLUE = CFSTR("BLUE");
// Set up the preference.
CFPreferencesSetAppValue(textColorKey, colorBLUE,
kCFPreferencesCurrentApplication);
// Write out the preference data.
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);

Reading a simple data from preference:

CFStringRef textColorKey = CFSTR("defaultTextColor");
CFStringRef textColor;
// Read the preference.
textColor = (CFStringRef)CFPreferencesCopyAppValue(textColorKey,
kCFPreferencesCurrentApplication);
// When finished with value, you must release it
// CFRelease(textColor);

To replace the exist key's value, just overwrite the values of key.

(Objective-C) Convert between NSString and CFStringRef

From NSString to CFStringRef:
NSString *test = @"test";
CFStringRef testCF = (CFStringRef) test;


From CFStringRef to NSString:
CFStringRef testCF = "test";
NSString *test = (NSString *) testCF;

Sep 1, 2008

End of student life

I just passed the presentation of my internship in Penbase, this means my student life is over, i finished my education of university, i am going to start the professional life.

Aug 13, 2008

(iPhone) How to use SQLite

1. Add libsqlite3.dylib framwork in your project.

2. Add #import in your interface file.

3. You should create a .sql file, there are many ways to do it: you can create with you console by taping sqlite3 file.sql or you can create by coding in your project. In my case, i create the database dynamically, so i create the database file in my code, and i will post an example below.

4. Declare 2 variables in your interface file:
sqlite3 *database;
NSString *dbPath;
In you implementation file:
- (void) createTable {
dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sql"];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *createSql = @"CREATE TABLE test (text varchar(255))";
if (sqlite3_exec(database, [createSql cStringUsingEncoding:NSUTF8StringEncoding], NULL, NULL, NULL) == SQLITE_OK) {
NSLog(@"create table");
NSString *insertSql = @"INSERT INTO test (text) VALUES('fff')";
int testvalue = sqlite3_exec(database, [insertSql cStringUsingEncoding:NSUTF8StringEncoding], NULL, NULL, NULL);
if (testvalue == SQLITE_OK) {
NSLog(@"insert query ok");
}
else {
NSLog(@"error code %i", testvalue);
}
}
}
}

and this method will show you how to use the select query:
- (void) findRowNb {
NSString *selectSql = @"SELECT COUNT(*) FROM test";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int count = sqlite3_column_int(statement, 0);
NSLog(@"row nb %i", count);
}
}
}

Aug 12, 2008

(Objective-C) How to use NSURLConnection

Create an object of NSMutableURLRequest, you can set the header field in this object. And then create an object of NSURLConnection with the parameter of NSMutableURLRequest and delelgate (its self).

The start method "start" is not necessary, in my case i got application crashed when i add start method. Don't forget set up a timer
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
after the start method.

The last thing is use the delegate methods of NSURLConnection, such as:
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
(void)connectionDidFinishLoading:(NSURLConnection *)connection

In these method, you can check the status of connection, received data and cancel the connectin in connectionDidFinishLoading.