2012-05-07 2 views
0

개체를 UIViewController을 통해 개체에 삽입해야하는 경우가 있습니다. 필자는 데이터베이스 모델 (엔티티 및 특성)을 설계했습니다. 엔티티를 UIViewController을 통해 추가하려고합니다. appDelegate.m의 didFinishLaunchingwithOptions 메소드에 무엇을 추가해야합니까? 보기 컨트롤러를 통해 엔티티에 데이터 추가

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

그리고 TUTViewController (내 자신의 뷰 컨트롤러 - 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); 
     }  
    } 
} 

내가 빠진 것이 있습니까? 나는 오류와 함께 종료 해요 :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'FetalData''

답변

0

당신이 코어 데이터 스택 또는 UIManagedDocument 객체를 설정 했습니까?

관리되는 개체 모델을 설정하지 않은 경우 문제가 될 수 있습니다. 즉, FetalData 엔티티를 정의하는 관리 객체 모델을로드하지 않는 것입니다. 자세한 내용은 insertnewobjectforentityforname을 참조하십시오.

저는 빈 프로젝트를 만들고 Xcode가 핵심 데이터를 생성하도록 제안합니다. 이 방법으로 코드가 어떻게 작동하는지 확인할 수 있습니다.

일부 주

왜 당신은 NSFetchResultsController를 사용합니까?

메서드가 끝날 때 save 호출로 이동하십시오. 이 방법으로 디스크로의 다중 라운드 트립을 피할 수 있습니다.

핵심 데이터 사용을 시작하려면 core-data-on-ios-5-tutorial-getting-started을 제안합니다.

희망이 있습니다.

관련 문제