开发者

Why is only the last record stored in the database?

I am doing tests in Core Data and have an array with approx. 230 records that i try to store in my database. Unfortunately only the last record seems to be stored in the database. I just can not figure out why as i can see that the while-loop is looping correctly?

Also, if one field (nrField) is indexed do i need to add numbers to it or will this happen automatically as i add records to the database? nrFields is the unique counter stored in each record.

//=========LOOP ARRAY AND INSERT RECORDS INTO DB==============//
while (eOF != 99999) {

    qNr = [myArray objectAtIndex:xx];
    eOF = [qNr intValue];
    xx = xx++;

    if (eOF != 99999) {

        qDifficulties = [myArray objectAtIndex:xx];
        int iDiff = [qDifficulties intValue];
        [famQuizInfo setValue:[NSNumber numberWithInt:iDiff] forKey:@"qDiff"];
        xx = xx++;

        // No action for Category as it is not included in the database any longer
        xx = xx++;  // Calculate +1 to compensate for non existing Category

        [famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"question"];
     开发者_JS百科   xx = xx++;

        [famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"qRightAnswer"];
        xx = xx++;

        [famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"qWrongAnswer1"];
        xx = xx++;

        [famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"qWrongAnswer2"];
        [famQuizInfo setValue:[NSNumber numberWithInt:0] forKey:@"qNrAccess"];
        [famQuizInfo setValue:[NSDate date] forKey:@"qRegDate"];


        //NSError *error;
        if (![qContext save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }

        xx = xx++;
        NSLog(@"xx: %i", xx);

    }
}


The problem is that you're updating a record and overwriting it within the loop. You need to create a fresh instance of famQuizInfo each time around the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜