我是新手,从头开始学习核心数据。 我已经设置了coredatamodel,然后创建了NSManagedObject类。然后在app委托中,我试图插入一些测试数据。但是,它无法正常工作。仅插入了最后一个数据。我应该放
[self saveContext];
在每个物体之间?在“applicationWillTerminate”方法中,调用saveContext方法,因此保存了最后一项。 (这是正确的吗?)
NSManagedObjectContext *context = [self managedObjectContext];
Vocabulary *vocabulary = [NSEntityDescription
insertNewObjectForEntityForName:@"Vocabulary"
inManagedObjectContext:context];
vocabulary.word = @"iPhone";
vocabulary.definition = @"better than Android";
vocabulary.level = @"beginner";
vocabulary.word = @"iPhone3gs";
vocabulary.definition = @"better than 3";
vocabulary.level = @"intermediate";
vocabulary.word = @"iPhone4";
vocabulary.definition = @"better than 3gs";
vocabulary.level = @"advanced";
vocabulary.word = @"iPhone4s";
vocabulary.definition = @"better than 4";
vocabulary.word = @"iPhone4s";
vocabulary.definition = @"64 is better than 32";
vocabulary.level = @"advanced";
答案 0 :(得分:0)
您需要为插入的每个词汇表对象插入一个新实体。所以这样做:
Vocabulary *vocabulary = nil;
NSManagedObjectContext *context = [self managedObjectContext];
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone";
vocabulary.definition = @"better than Android";
vocabulary.level = @"beginner";
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone3gs";
vocabulary.definition = @"better than 3";
vocabulary.level = @"intermediate";
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone4";
vocabulary.definition = @"better than 3gs";
vocabulary.level = @"advanced";
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone4s";
vocabulary.definition = @"better than 4";
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone4s";
vocabulary.definition = @"64 is better than 32";
vocabulary.level = @"advanced";