2011-11-09 59 views
0

這裏是我的代碼:錯誤而coredata節省對象

- (IBAction) saveData 
{ 
    NSLog(@"saveData"); 
    [self dismissKeyboard]; 
    Fugitive *job = (Fugitive *)[NSEntityDescription insertNewObjectForEntityForName:@"Fugitive" inManagedObjectContext:managedObjectContext]; 
    job.name = txtName.text; 



    NSError *error; 

    // here's where the actual save happens, and if it doesn't we print something out to the console 
    if (![managedObjectContext save:&error]) 
    { 
     NSLog(@"Problem saving: %@", [error localizedDescription]); 
    } 

    // **** log objects currently in database **** 
    // create fetch object, this object fetch's the objects out of the database 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fugitive" inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 
    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 

    NSURL *storeURL = [[NSBundle mainBundle] URLForResource:@"iBountyHunter" withExtension:@"momd"]; 

    id globalStore = [[managedObjectContext persistentStoreCoordinator] persistentStoreForURL:storeURL]; 


    [managedObjectContext assignObject:job toPersistentStore:globalStore]; 

    for (NSManagedObject *info in fetchedObjects) 
    { 
     NSLog(@"Job Name: %@", [info valueForKey:@"name"]); 

    } 
    [fetchRequest release]; 
[self.navigationController dismissModalViewControllerAnimated:YES]; 
} 

在控制檯中我得到這個錯誤:問題保存:操作無法完成。 (可可錯誤1570) 在tableview中創建新的對象,但不能因此保存重新啓動應用程序時,該物體消失

+0

賠率是你在你的核心數據堆棧有配置問題。發佈您設置模型,持久存儲和上下文的代碼。另外,如果您已經配置了上下文以使商店已經存在,並且您的應用只有一個商店,則不需要執行'assignObject:toPersistentStore:'。 – XJones

+0

我已經發布了代碼作爲答案,它太長了把它放在這裏 –

回答

-1
- (void)saveContext 
{ 

    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 

    if (managedObjectContext != nil) 
    { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) 
     { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
} 


- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (__managedObjectContext != nil) 
    { 
     return __managedObjectContext; 
    } 

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

- (NSManagedObjectModel *)managedObjectModel 
{ 
    if (__managedObjectModel != nil) 
    { 
     return __managedObjectModel; 
    } 
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"iBountyHunter" withExtension:@"momd"]; 
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];  
    return __managedObjectModel; 
} 

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (__persistentStoreCoordinator != nil) 
    { 
     return __persistentStoreCoordinator; 
    } 

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"iBountyHunter.sqlite"]; 

    NSError *error = nil; 

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 


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



    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
    { 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return __persistentStoreCoordinator; 
} 
+0

很高興給一些解釋以及一大堆舊代碼! – jrturton

+0

這是解決方案,還是更多的問題? –