开发者

why is this loop eleven times?

given the code i have with Core Data, why is below code loop 11 rounds?

for (NSManagedObject *info in fetchedObjects) {
    xx = xx + 1;
    NSLog(@"\n \n");
    NSLog(@"id: %i", [[info valueForKey:@"idQ"] intValue]);
    NSLog(@"qDiff: %@", [info valueForKey:@"qDiff"]);
    NSLog(@"question: %@", [info valueForKey:@"question"]);
    N开发者_StackOverflow中文版SLog(@"qRightAnswer: %@", [info valueForKey:@"qRightAnswer"]);
    NSLog(@"qWrongAnswer1: %@", [info valueForKey:@"qWrongAnswer1"]);
    NSLog(@"qWrongAnswer2: %@", [info valueForKey:@"qWrongAnswer2"]);
    NSLog(@"qNr: %@", [info valueForKey:@"qNr"]);
    NSLog(@"qRegDate: %@", [info valueForKey:@"qRegDate"]);
    NSLog(@"\n \n");
    NSLog(@"nr: %i", xx);
}  

Here is the code:

- (void)testingDBmodel {

NSLog(@">>testingDBmodel<<");

//=================DATABASE===================//
// id
// qDiff
// question
// qRightAnswer
// qWrongAnswer1
// qWrongAnswer2
// qNr
// qRegDate

if (managedObjectContext == nil) { managedObjectContext = [(FamQuiz_v2AppDelegate *)
                                [[UIApplication sharedApplication] delegate] managedObjectContext]; }


NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *famQuizInfo = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"questions" 
                                   inManagedObjectContext:context];
[famQuizInfo setValue:[NSNumber numberWithInt:1] forKey:@"idQ"];
[famQuizInfo setValue:@"qDiff1" forKey:@"qDiff"];
[famQuizInfo setValue:@"question1" forKey:@"question"];
[famQuizInfo setValue:@"qRightAnswer1" forKey:@"qRightAnswer"];
[famQuizInfo setValue:@"qWrongAnswer1_1" forKey:@"qWrongAnswer1"];
[famQuizInfo setValue:@"qWrongAnswer2_2" forKey:@"qWrongAnswer2"];
[famQuizInfo setValue:@"999" forKey:@"qNr"];
[famQuizInfo setValue:[NSDate date] forKey:@"qRegDate"];

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


//==========READ DATABASE==============//
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"questions" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

NSLog(@"\n \n");
NSLog(@"\n \n");

// NSLog(@"fetchObjects#: %i", [fetchedObjects.count]); NSLog(@"\n \n"); NSLog(@"fetchedObjects: %@", fetchedObjects); NSLog(@"\n \n");

int xx = 0; // just to see how many loops

for (NSManagedObject *info in fetchedObjects) {
    xx = xx + 1;
    NSLog(@"\n \n");
    NSLog(@"id: %i", [[info valueForKey:@"idQ"] intValue]);
    NSLog(@"qDiff: %@", [info valueForKey:@"qDiff"]);
    NSLog(@"question: %@", [info valueForKey:@"question"]);
    NSLog(@"qRightAnswer: %@", [info valueForKey:@"qRightAnswer"]);
    NSLog(@"qWrongAnswer1: %@", [info valueForKey:@"qWrongAnswer1"]);
    NSLog(@"qWrongAnswer2: %@", [info valueForKey:@"qWrongAnswer2"]);
    NSLog(@"qNr: %@", [info valueForKey:@"qNr"]);
    NSLog(@"qRegDate: %@", [info valueForKey:@"qRegDate"]);
    NSLog(@"\n \n");
    NSLog(@"nr: %i", xx);
}        
[fetchRequest release];

//NSError* error;
if(![[famQuizInfo managedObjectContext] save:&error]) {
    NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
    NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
    if(detailedErrors != nil && [detailedErrors count] > 0) {
        for(NSError* detailedError in detailedErrors) {
            NSLog(@"  DetailedError: %@", [detailedError userInfo]);
        }
    }
    else {
        NSLog(@"  %@", [error userInfo]);
    }
}

}

Here is the two last loops of output:

2010-12-03 17:37:51.962 Test_v2[11965:207] id: 1

2010-12-03 17:37:51.963 Test_v2[11965:207] qDiff: qDiff1

2010-12-03 17:37:51.964 Test_v2[11965:207] question: question1

2010-12-03 17:37:51.964 Test_v2[11965:207] qRightAnswer: qRightAnswer1

2010-12-03 17:37:51.964 Test_v2[11965:207] qWrongAnswer1: qWrongAnswer1_1

2010-12-03 17:37:51.965 Test_v2[11965:207] qWrongAnswer2: qWrongAnswer2_2

2010-12-03 17:37:51.966 Test_v2[11965:207] qNr: 999

2010-12-03 17:37:51.967 Test_v2[11965:207] qRegDate: 2010-12-03 17:36:12 +0100

2010-12-03 17:37:51.967 Test_v2[11965:207]

2010-12-03 17:37:51.973 Test_v2[11965:207] nr: 10

2010-12-03 17:37:51.973 Test_v2[11965:207]

2010-12-03 17:37:51.974 Test_v2[11965:207] id: 1

2010-12-03 17:37:51.974 Test_v2[11965:207] qDiff: qDiff1

2010-12-03 17:37:51.975 Test_v2[11965:207] question: question1

2010-12-03 17:37:51.976 Test_v2[11965:207] qRightAnswer: qRightAnswer1

2010-12-03 17:37:51.976 Test_v2[11965:207] qWrongAnswer1: qWrongAnswer1_1

2010-12-03 17:37:51.977 Test_v2[11965:207] qWrongAnswer2: qWrongAnswer2_2

2010-12-03 17:37:51.977 Test_v2[11965:207] qNr: 999

2010-12-03 17:37:51.978 Test_v2[11965:207] qRegDate: 2010-12-03 17:37:51 +0100

2010-12-03 17:37:51.978 Test_v2[11965:207]

2010-12-03 17:37:51.979 Test_v2[11965:207] nr: 11


Not even looking at your code, the output tells us that there are at least 2 different objects in the database: One with a qRegDate: 2010-12-03 17:37:51 +0100 and one with qRegDate: 2010-12-03 17:36:12 +0100. The assumption would be that you actually have more than 2 (say, 11) rows in your db. To check that, run the app on the simulator and open the database file stored in ~/Library/Application Support/iPhone Simulator/-simulator version-/Applications/-some long number in the Documents directory.

Looking into the code a bit more, you're opening a Context, writing to it, than reading from it, than saving it. Each time you run this code, you'll add one more object to it thanks to using the current data as the RegDate.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜