Murat Kaya
Murat Kaya

Reputation: 1321

FMResultset to Nsarray

I have 2035 object in my sqlite database. And I am trying to show them in tableview.I am adding objects to NSMutablearray with this code.

FMResultSet *rs = [database executeQuery:@"SELECT name,tel1 FROM KartDB"];
    KartList *kart = [[KartList alloc] init];
        while ([rs next]) {
            kart.name = [rs stringForColumn:@"name"];
            kart.tel1 = [rs stringForColumn:@"tel1"];

            [_customers addObject:kart];
        }

I put breakpoint to [_customers addObject:kart]; line. Its replacing all values every loop.What should I need for this.

Thanks.

Upvotes: 1

Views: 252

Answers (1)

ogres
ogres

Reputation: 3690

You are using the same kart object each time, and changing its values and adding to the array, it will always add the same Object and at the end you`ll have the same object 2035 times with latest values,

You must create kart object for each row

FMResultSet *rs = [database executeQuery:@"SELECT name,tel1 FROM KartDB"];

    while ([rs next]) {
        // Inside loop
        KartList *kart = [[KartList alloc] init];
        kart.name = [rs stringForColumn:@"name"];
        kart.tel1 = [rs stringForColumn:@"tel1"];

        [_customers addObject:kart];
    }

Upvotes: 1

Related Questions