【发布时间】:2014-04-12 09:53:56
【问题描述】:
所以我似乎无法在我的表格视图中显示任何数据,但后来我注意到上下文没有保存并且我一直收到此错误:
[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x166a70b0) NO CHANGES IN ** DEFAULT ** CONTEXT - NOT SAVING
2014-04-12 19:49:04.722 0DataCollector[3218:60b] Managedcontext working (
TestSuburb
)
- (IBAction)backToSuburbsTableViewController:(UIStoryboardSegue *)segue {
if ([segue.sourceViewController isKindOfClass:[NewSuburbTableViewController class]]) {
NSLog(@"Coming back from NewSuburbTableViewController");
NewSuburbTableViewController *srcVC = segue.sourceViewController;
suburbString = srcVC.suburbTextField.text;
[suburbsArray addObject:suburbString];
//Save Managed Object Context
[[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"You successfully saved your context.");
} else if (error) {
NSLog(@"Error saving context: %@", error.description);
}
}]; NSLog(@"Managedcontext working %@", suburbsArray);
}
您能提供的任何帮助将不胜感激,我也尝试保存没有帮助的“contextWithinCurrentThread”。
TableViewController 的其余代码:
- (void)viewDidLoad
{
[super viewDidLoad];
suburbsArray = [[NSMutableArray alloc] init];
[self fetchSuburbs];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)fetchSuburbs {
//Fetch suburbs
self.suburbsArray = [NSMutableArray arrayWithArray:[Suburb findAllSortedBy:@"name" ascending:YES]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self fetchSuburbs];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [suburbsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSMutableArray *reversedSuburbsArray = [[[suburbsArray reverseObjectEnumerator] allObjects] mutableCopy];
//Fetch suburb
Suburb *suburb = [self.suburbsArray objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = suburb.name;
return cell;
}
【问题讨论】:
-
这似乎不是错误,它是预期的 NSLog 输出。你在重新加载你的表吗?你怎么确定它没有被保存?
-
感谢院长的快速回复。数据没有被保留,如果我退出应用程序并返回它就消失了。如果我在这里做一些根本错误的事情,请随时指出这一点。
-
是的,我“认为”我正在正确地重新加载它,我将编辑帖子以显示数据重新加载。
-
您必须关闭 MR 日志记录,或者您可能没有显示所有控制台输出。你知道如何打开/关闭该选项吗?
-
显示您在哪里创建 NSmanagedObject 并将其插入到您的上下文中。
标签: ios core-data magicalrecord