2012-02-17 6 views
0

私は最初から初心者でコアデータを学習しています。 私はcoredatamodelをセットアップしてから、NSManagedObjectクラスを作成しました。その後、アプリケーションデリゲートでいくつかのテストデータを挿入しようとしました。しかし、正しく動作しませんでした。最後のデータのみが挿入されました。私は置くべきですか?コアデータを使用した複数のテストデータの追加

[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

挿入する各ボキャブラリオブジェクトに新しいエンティティを挿入する必要があります。そう:

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"; 
+0

ありがとうTim。できます! – Vector

関連する問題