views:

28

answers:

1

greetings Cocoa masters - this simple issue has me crawling the walls. I have a custom class called Movie which consists of a bunch of properties and a few collections. I am populating it successfully using FMDB and SQLite. However, with each pass through the result collection from the DB, my addObject: seems to be writing over the entire array:

SciFiLib = [[NSMutableArray alloc]init];
FMResultSet *SciFiResultSet = [db executeQuery:@"select Movie.*......];
Movie *m = [[Movie alloc] init];

while ([SciFiResultSet next]) {
m.movieID =[SciFiResultSet stringForColumn:@"movie_id"];
m.title = [SciFiResultSet stringForColumn:@"title"];
.....
[SciFiLib addObject: m];

At this point - I have NSLog'd the output of m - and it contains a different movie (title, ID, release date, etc. - so I know the data is OK). However, starting with the 2nd pass through the WHILE loop each subsequent addObject replaces the entire array with copies of the next data item. So at the end of my loop, I have 6 copies of the same movie data.

I have mirrored my custom class here with just an array of the movie titles, and that seemed to work, but I'd like to collect all of the properties of the movie(s) for my Model data. Can anyone shed some light on what might be causing this behavior?

Thanks in advance for your help and advice!

...

+5  A: 

I think it's because you're just adding a pointer, and then reapplying the data to the same object "m". Make "m" inside the loop and release it so it gets remade each time.

UltimateBrent
Bingo. You win the door prize. Amazing response time BTW. Thanks a TON - no seriously, a metric ton.
adickers
Accept the answer then :D click the check.
UltimateBrent