将对象从一个 Core Data UITableView 移动到另一个?
Posted
技术标签:
【中文标题】将对象从一个 Core Data UITableView 移动到另一个?【英文标题】:Move object from one Core Data UITableView to another? 【发布时间】:2010-03-02 12:07:43 【问题描述】:我正在尝试将对象从 fetchedresultscontroller 移动到另一个。代码如下所示:
UISegmentedControl *segmentedControl = self.navigationItem.titleView;
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
NSDate *timeValue = [[managedObject valueForKey:@"timeStamp"] copy];
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error = nil;
if (![context 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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
[self.tableView reloadData];
fetchedResultsController = [fetchedResultsControllers objectAtIndex:(1-segmentedControl.selectedSegmentIndex)];
context = [fetchedResultsController managedObjectContext];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Picked" inManagedObjectContext:context];
[newManagedObject setValue:timeValue forKey:@"timeStamp"];
error = nil;
if (![context 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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
fetchedResultsController = [fetchedResultsControllers objectAtIndex:(1-segmentedControl.selectedSegmentIndex)];
但它不起作用。我得到了可怕的:
Serious application error. Exception was caught during Core Data change processing: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted). with userInfo (null)
我真的不知道如何让它工作。 Core Data 很难使用。
【问题讨论】:
【参考方案1】:我昨晚自己遇到了这个问题。就我而言,我忘记实现 NSFetchedResultsControllerDelegate
方法:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath;
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type;
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller;
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller;
问题是这样的:在您删除 NSManagedObjectContext
中的对象,然后保存上下文后,UITableView
与更改不同步,并要求不存在的项目。
CoreDataBooks 示例代码展示了如何结合在 NSFetchedResultsController
中添加/删除托管对象来实现这些功能。
【讨论】:
以上是关于将对象从一个 Core Data UITableView 移动到另一个?的主要内容,如果未能解决你的问题,请参考以下文章
从内存存储中删除 Core Data 对象会将它们变成故障,但不会擦除它们
当用户滚动 MKMapView 时,从 Core Data 加载其他对象
将对象添加到 NSSet 时与 Core Data 的反比关系导致崩溃