作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对核心数据使用单例。这是核心数据堆栈:
#pragma mark - Core Data stack
- (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:@"Model" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
这是我用来保存实体的方法,并且它有效
-(void)addFund {
Fund *fund = [NSEntityDescription insertNewObjectForEntityForName:@"Fund" inManagedObjectContext:self.managedObjectContext];
fund.name = @"Test Name";
[self.managedObjectContext save:nil]; // write to database
}
当我需要在 Controller 中显示表中的项目时,问题就出现了。我创建了一个返回 NSFetchedResultsController 的方法。可能这就是错误。
-(NSFetchedResultsController*)loadFunds {
//Core data request
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fund"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
NSFetchedResultsController *result = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
return result;
}
但 TableView 不返回任何内容(Null):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger numberOfSections = [[[[Singleton sharedSingleton] loadFunds] sections] count];
if (numberOfSections == 0) {
numberOfSections = 1;
}
return numberOfSections;
}
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
id sectionInfo = [[[Singleton sharedSingleton].loadFunds sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Category Cell";
....
// Configure the cell...
Fund *fund = [[Singleton sharedSingleton].loadFunds objectAtIndexPath:indexPath];
cell.titleLabel.text = fund.name;
....
}
非常感谢您的帮助!
最佳答案
您需要使用以下方式获取数据:
NSError *error;
[self.fetchedResultsController performFetch: &error];
然后重新加载表格 View ,如下所示:
[self.tableView reloadData];
关于ios - 单例核心数据方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14591395/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!