2012-12-11 2 views
0

내 응용 프로그램에 핵심 데이터를 통합 할 때 이미 sqllite DB 파일이 있습니다. 더 쉽게 만들기 위해 새로운 것을 만들어야합니까, 아니면 기존의 것을 사용해야합니까? 많은 질문에 유감스럽게 생각합니다. 진심으로 감사드립니다 !!기존 응용 프로그램에 coredata 구현, sqllite 만들기 또는 autowritten

또한 어떻게 새 것을 만들 수 있습니까?

나는 그러나 내가 텍스트 필드

이 무엇

에 무엇을 넣어 모르는, AppDelegate에 (오류없이)에 아래의 방법을 구현? "myCoreData"는 .xcdatamodeld로 끝나는 코어 데이터 db의 이름입니까? 그렇다면, momd는 무엇입니까?

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myCoreData" withExtension:@"momd"]; 

이것은 무엇입니까? 이렇게하면 데이터베이스가 생성됩니까? 아니면 데이터베이스를 만들고 여기에 정보를 입력해야합니까? 이 파일은 어디에 저장되어 있습니까?

NSURL *storeURL = [[self applicationDocumentsDirectoryModified] URLByAppendingPathComponent:@"coreDataDB.sqlite"]; 

는 구현에게 있습니다

- (void)saveContext 
{ 
    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    if (managedObjectContext != nil) { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { 
      // Replace this implementation with code to handle the error appropriately. 
      // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
} 

#pragma Core Data stack 

// Returns the managed object context for the application. 
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (_managedObjectContext != nil) { 
     return _managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) { 
     _managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [_managedObjectContext setPersistentStoreCoordinator:coordinator]; 
    } 
    return _managedObjectContext; 
} 

// Returns the managed object model for the application. 
// If the model doesn't already exist, it is created from the application's model. 
- (NSManagedObjectModel *)managedObjectModel 
{ 
    if (_managedObjectModel != nil) { 
     return _managedObjectModel; 
    } 
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myCoreData" withExtension:@"momd"]; 
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 
    return _managedObjectModel; 
} 

// Returns the persistent store coordinator for the application. 
// If the coordinator doesn't already exist, it is created and the application's store added to it. 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (_persistentStoreCoordinator != nil) { 
     return _persistentStoreCoordinator; 
    } 

    NSURL *storeURL = [[self applicationDocumentsDirectoryModified] URLByAppendingPathComponent:@"coreDataDB.sqlite"]; 

    NSError *error = nil; 
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { 

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

    return _persistentStoreCoordinator; 
} 
+0

그냥 마법의 레코드를 사용하십시오. 그것은 최고이며 보일러 플레이트 코드를 처리합니다. 여기 예가 있습니다 : http://yannickloriot.com/2012/03/magicalrecord-how-to-make-programming-with-core-data-pleasant/ – mkral

+0

이 재미있어 보이지만 이것을 사용하기 전에 핵심 데이터를 배우고 싶습니다. –

+0

그냥 발을 젖게하는 경우. 그러나 프로덕션 응용 프로그램에서 스레딩을 사용하는 경우 MR을 강력히 권장합니다. 그것은 당신의 삶을 훨씬 심플하게 만듭니다. – mkral

답변

1

엑스 코드에서 새 프로젝트에서 상용구 코드를 추가하는 최대 종단 마법 기록을 사용하지 않았다. 유사보기 응용 프로그램을 시작하고, coredata를 클릭하고, adddelegate로 이동하십시오. 맨 밑에 필요한 방법이 있습니다.

쉽게 만들 수 있도록 필자는 필 요에 필요한 모든 방법을 사용하여 커뮤니 케 이터를 만들었습니다.

관련 문제