Search code examples
objective-ccocoafmdb

Why is this instance object being changed?


I'm very new at objective C, I'm just learning. I did the techotopia tutorial "An_Example_SQLite_based_iOS_4_iPhone_Application_(Xcode_4)", then tried to implement it again with FMDB. (I'd post the link to the tutorial but it let's me only post 2 links max)

The problem: In initWithFrame I create eventDB. Then in addEvent, after a keypress, the eventDB.database's contents are changed. This is eventDB in initWithFrame and this is it in addEvent.

#import "appTracker.h"
@implementation appTracker

- (id) initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    eventDB = [[appTrackerDB alloc] init];
    return self;
}


- (void) keyDown: (NSEvent *) event
{

    NSString *chars = [event characters];
    unichar character = [chars characterAtIndex: 0];
    if (character == 'A') {
        NSLog (@"Adding event");
        [self addEvent:@"test_arg"];            
    }
}

- (void) addEvent: (NSString *) name
{
    [eventDB setName:name];
    [eventDB setPhone:name];
    [eventDB setAddress:name];
    [eventDB setStatus:name];
    [eventDB saveData];
}
...
@end

Using GDB I stepped through and found that it is changing in main.m (autogenerated by XCode4) here: (not really sure what this code does or why it's there)

    #import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
{
    return NSApplicationMain(argc, (const char **)argv);
}

I'm unfamiliar with objective C. Can someone help me figure out why my eventDB.database object is being changed? I'm probably not managing some memory correctly or totally misinterpreting how you are supposed to do this. Any help would be appreciated.

eventDB is an instance of:

#import <Foundation/Foundation.h>
#import "FMDatabase.h"

@interface appTrackerDB : NSObject {
    NSString *name;
    NSString *address;
    NSString *phone;
    NSString *status;
    NSString *databasePath;
    FMDatabase *database;
}

Thanks!

Also [eventDB saveData] is:

- (void) saveData
{
    [database executeUpdate:@"insert into user (name, address, phone) values(?,?,?)",
     name, address, phone,nil];
}

And created the database with:

@implementation appTrackerDB
@synthesize name,address,status,phone;

- (id)init
{
    self = [super init];
    if (self) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docsPath = [paths objectAtIndex:0];
        NSString *path = [docsPath stringByAppendingPathComponent:@"database.sqlite"];

        database = [FMDatabase databaseWithPath:path];
        [database open];

        [database executeUpdate:@"create table IF NOT EXISTS user(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)"];
        if ([database hadError]) {
            NSLog(@"DB Error %d: %@", [database lastErrorCode], [database lastErrorMessage]);
        }
        name = @"TEST";
    }

    return self;
}

Solution

  • You don't actually retain the Database. In Objective-C you need to manually retain the Objects, especially if they are not properties. (e.g. name is declared as a property, database is not)

    Retaining means that you own the Object. databaseWithObject retains the Database but calls autorelease on it, which normally means, it will delete the reference as soon as possible after the calling method is finished.

    Depending on your platform, e.g. OS X instead of iOS, you could enable the GarbageCollection-feature. This would mean, that the OSX/Objective-C environment would do a lot of the memory management for you. But for this to be of any use, you would need to the declare the pertaining instance variables as properties and use the appropriate setter- and getter-methods on them.

    Here is an example of a property-declaration (appTrackerDB.h):

    #import <Foundation/Foundation.h>
    #import "FMDatabase.h"
    
    @interface appTrackerDB : NSObject {
      /*
        These are only necessary when 
        using iOS-Versions prior to
        iOS 4, or if you really
        need to manipulate the values 
        without utilizing the setter-/getter-
        methods.
      */
      NSString *name;
      NSString *address;
      NSString *phone;
      NSString *status;
      NSString *databasePath;
      FMDatabase *database;
    }
    @property (retain) NSString *name,*address;
    @property (retain) NSString *phone,*status,*databasePath;
    @property (retain) FMDatabase *database;
    

    appTrackerDB.m:

    @implementation appTrackerDB
    @synthesize name,address,status,phone;
    @synthesize databasePath,database;
    

    An example setter method you would call instead of you manual assignment is:

    • [self setDatabase:...]; instead of assigning a value directly database = ...

    Setter methods like setVariableName and getter methods like variableName are synthesized for you by the @synthesize directive.