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!

...

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.