2012-06-07 131 views
0

大家好我有使用CoreData持久性的问题,我的问题是,当我启动我的应用程序管理与使用的NSLog添加一些数据(从应用程序内的表单)到我的数据库并显示出来。 但实际上,我认为所有这些数据在我停止ipad模拟器并重新启动后消失..数据持久化与CoreData

所以我真的不知道它是否来自我的代码或者是因为模拟器。 我做了一个图给你看我的应用程序的架构和我的实体:

model

model

的问题是,我使用不同的viewController,所以我需要通过ManagedObjectModel每个一。我的表单在newDocumentViewController中,当我添加索姆实体时,我想在所有其他viewController中访问它们并将其保存到应用程序本地存储中。

下面是一些代码向你展示了一下:

AppDelegate.m

@synthesize managedObjectContext = __managedObjectContext; 
@synthesize managedObjectModel = __managedObjectModel; 
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator; 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 

    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 
    UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController]; 

    MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil]; 
    UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController]; 

    masterViewController.managedObjectContext = self.managedObjectContext;  
    detailViewController.managedObjectContext = self.managedObjectContext; 

我有每个masterViewController和DetailViewController(从DetailViewController到NewDocumenViewController)内这些属性来接收的ObjectContext

@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext; 

所以有了这个,我真的不知道如何从每个控制器访问我的数据,并且将数据存储LO凯莉做这样的:

NewDocumentController.m

-(void) addNewDocument:(NSString*)name with_niveau:(NSInteger)level{ 
    Document *doc = [NSEntityDescription insertNewObjectForEntityForName:@"Document" inManagedObjectContext:managedObjectContext]; 
    doc.nom=name; 
    doc.niveau=[NSNumber numberWithInteger:level]; 
} 
-(void) addNewDocument_info:(NSString*)name with_createur:(NSString*)createur with_dateModif:(NSDate*)date1 with_status:(BOOL)etat{ 

    DocumentInfo *doc_info = [NSEntityDescription insertNewObjectForEntityForName:@"DocumentInfo" inManagedObjectContext:managedObjectContext]; 

    doc_info.nom =name; 
    doc_info.createur=createur; 
    doc_info.date_creation=[NSDate date]; 
    doc_info.date_modification=date1; 
    doc_info.status= [NSNumber numberWithBool:etat]; 
} 

回答

0

您需要保存的数据:

NSError *error = nil; 
[self.managedObjectContext save:&error]; 
+0

笑!简单的回答,以巨大的问题 – QED

+0

我发现它几分钟前正义谢谢你回答:) – Bobyblanco