我有一个使用 CoreData 数据库的 TableViewController。我有另一个 UIviewController,我想从中读取 TableViewController 的数据库。我所做的如下。
//In UIviewController
-(NSArray *)fetchRecordedDatainsqldatabase
{
// construct a fetch request
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TrackerList" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
//[fetchRequest setFetchBatchSize:20];
// Create the sort descriptors array.
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"descript" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:descriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// return the result of executing the fetch request
return [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];}
我有一个财产
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
But managedObjectContext is always nil, at the line
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TrackerList"
inManagedObjectContext:self.managedObjectContext];
因此,当程序到达该行时,它总是崩溃。可能是什么问题呢?