+ entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称'Todo''

时间:2014-01-31 01:20:38

标签: ios iphone objective-c core-data

这是我的代码,它说+ entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称'Todo''。我不知道如何解决它。

(NSFetchedResultsController *)fetchedResultsController

{
        if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Todo" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];


// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"order" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];



// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

2 个答案:

答案 0 :(得分:0)

Self.managedobjectcontext是零,所以找出原因并修复

答案 1 :(得分:0)

您可能需要设置托管对象上下文。因此,在您提出请求的地方之上,请执行类似

的操作
// Set up our RestKit stores
RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
self.managedObjectContext = [managedObjectStore persistentStoreManagedObjectContext];

所以我的整个请求方法看起来像

- (void)loadPosts
{
// Set up our RestKit stores
RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
self.managedObjectContext = [managedObjectStore persistentStoreManagedObjectContext];

// Make a RestKit request
[[RKObjectManager sharedManager] getObjectsAtPath:@"/api/posts/"
                                       parameters:nil
                                          success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                              NSLog(@"%@ %@", operation, mappingResult);
                                              [self.refreshControl endRefreshing];
                                          }
                                          failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                              [self.refreshControl endRefreshing];
                                              NSLog(@"Error: %@", [error localizedDescription]);
                                          }];

}