0

を介してオブジェクトをエンティティに挿入する必要がある場合がありますUIViewController。データベース モデル (エンティティと属性) を設計しました。を介してエンティティを追加していUIViewControllerます。didFinishLaunchingwithOptionsappDelegate.mのメソッドに何を追加すればよいですか?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch   
    return YES;
}

そして、TUTViewController(私自身のView Controller - UIViewController)オブジェクトを挿入するために以下のコードを使用しました。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    NSString *stripped1 = [response stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    NSMutableArray *rows = [NSMutableArray arrayWithArray:[stripped1 componentsSeparatedByString:@"\n"]];
    NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:[rows count]]; 
    NSMutableArray *contentArray1 = [NSMutableArray arrayWithCapacity:[rows count]];

    NSArray *components;
    NSLog(@"count:%d",[rows count]);

    for (int i=0;i<[rows count]; i++) {

       if(i == 0 || [[rows objectAtIndex:i] isEqualToString:@""]){
          continue;
       }

       components = [[rows objectAtIndex:i] componentsSeparatedByString:@","];
       id x = [components objectAtIndex:0] ;
       id y = [components objectAtIndex:1];
       id z = [components objectAtIndex:2];

       [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"X",y,@"Y", nil]];
       [contentArray1 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"X",z,@"Y", nil]];

       [newManagedObject setValue:[x] forKey:@"timeStamp"];
       [newManagedObject setValue:[y] forKey:@"beat"];
       [newManagedObject setValue:[z] forKey:@"rate"];

       // Save the context.
       NSError *error = nil;
       if (![context save:&error]) {

           NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
           abort();

           NSLog(@"Contents of Uterus Contraction: %@",contentArray);
           NSLog(@"Contents of Heart Beat: %@",contentArray1);
       }    
    }
}

不足しているものはありますか?私はエラーで終わっています:

キャッチされない例外 'NSInternalInconsistencyException' が原因でアプリを終了しています。理由: '+entityForName: エンティティ名 'FetalData' の NSManagedObjectModel が見つかりませんでした''

4

1 に答える 1

0

UIManagedDocumentコア データ スタックまたはオブジェクトをセットアップしましたか?

管理オブジェクト モデルを設定していない場合、これが問題になる可能性があります。FetalDataこれは、エンティティを定義するマネージド オブジェクト モデルを読み込んでいない可能性があることを意味します。詳細については、 insertnewobjectforentityfornameを参照してください。

空のプロジェクトを作成し、Xcode に Core Data を作成させることを強くお勧めします。このようにして、コードがどのように機能するかを確認できます。

いくつかのメモ

を使用するのはなぜNSFetchResultsControllerですか?

saveメソッドの最後に呼び出しを移動します。このようにして、ディスクへの複数回の往復を回避します。

Core Data の使用を開始する場合は、core-data-on-ios-5-tutorial-getting-startedをお勧めします。

それが役に立てば幸い。

于 2012-05-07T08:25:53.953 に答える