我正在尝试通过此代码在 Core Data 中插入一些对象
- (void)insertNewObject: (NSArray *) userInfo // Заносит блюдо в локальную базу данных
{
for (int i = 0; i < userInfo.count; i++) {
billContent * bc = [userInfo objectAtIndex:i];
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *nmo = [NSEntityDescription insertNewObjectForEntityForName:[entity name]
inManagedObjectContext:context];
[nmo setValue:CountID forKey:@"billId"];
[nmo setValue:bc.billAmount forKey:@"courseCount"];
[nmo setValue:bc.billCourseId forKey:@"courseId"];
[nmo setValue:bc.billPrice forKey:@"coursePrice"];
[nmo setValue:bc.billTitle forKey:@"courseTitle"];
[self saveContext];
}
}
- (void)saveContext {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
如何在我的表格视图中查看这些对象?
在插入后,我试图通过这段代码从核心数据中获取
[self makeAList];
和这里
-(void) makeAList {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
for (int i = 0; i < [sectionInfo numberOfObjects]; i ++) {
NSIndexPath *ip = [NSIndexPath indexPathForRow: i inSection:0];
billContent *bc = [[billContent alloc] init];
NSManagedObject *mo = [self.fetchedResultsController objectAtIndexPath:ip];
bc.billId = [[mo valueForKey:@"billId"] description];
bc.billTitle = [[mo valueForKey:@"courseTitle"] description];
bc.billPrice = [[mo valueForKey:@"coursePrice"] description];
bc.billAmount = [[mo valueForKey:@"courseCount"] description];
bc.depId = [[mo valueForKey:@"departmentId"] description];
bc.billCourseId = [mo valueForKey:@"courseId"];
[saved addObject:bc];
}
[countView1 reloadData];
}
但没有任何效果。但是如果我退出这个类,并再次输入它(加载 viewDidLoad)我可以看到新值,我做错了什么?