CoreData 데이터베이스를 사용하는 TableViewController가 있습니다. TableviewController의 데이터베이스를 읽고 싶은 또 다른 UIviewController가 있습니다. 내가 한 일은 다음과 같습니다.TableViewController의 CoreData 데이터베이스에서 데이터를 가져 오는 중
//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];
의 속성이 있습니다. 무엇이 문제 일 수 있습니까?
NSManagedObjectContext는 어디에 생성합니까? – ColinE
ViewDidLoad에서 _managedObjectContext = [[NSManagedObjectContext alloc] init]; – Bryanyan
_managedObjectContext = [[NSManagedObjectContext alloc] init]을 입력하면 문제가 발생합니다. ViewDidLoad에서 _managedObjectContext는 nil이 아닙니다. 그러나 프로그램이 줄에 도달하면 충돌이 발생합니다. NSEntityDescription * entity = [NSEntityDescription entityForName : @ "TrackerList" inManagedObjectContext : self.managedObjectContext]; – Bryanyan