为什么这个循环十一次?

时间:2010-12-03 16:44:48

标签: iphone objective-c

根据我对Core Data的代码,为什么下面的代码循环11轮呢?

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);
}  

以下是代码:

- (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]);
    }
}

}

这是输出的最后两个循环:

  

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]提问:问题1   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]问题:问题1 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

1 个答案:

答案 0 :(得分:0)

甚至没有看你的代码,输出告诉我们数据库中至少有两个不同的对象:一个有qRegDate:2010-12-03 17:37:51 +0100和一个qRegDate:2010- 12-03 17:36:12 +0100假设你的数据库实际上有超过2行(比如11行)。要检查这一点,请在模拟器上运行应用程序,然后打开存储在Documents目录中~/Library/Application Support/iPhone Simulator/-simulator version-/Applications/-some long number中的数据库文件。

进一步研究代码,你打开一个上下文,写入它,而不是从中读取,而不是保存它。每次运行此代码时,由于使用当前数据作为RegDate,您将再添加一个对象。