0

我总是以同样的方式从我的网络服务中获取我的数据。它总是有效的。但现在我收到了这个错误。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter
searching for entity name 'Team''

它在这种方法中崩溃。

- (NSMutableArray *)getDoelmannen
{
    NSLog(@"tot hier doelman");
    NSArray *results = [[NSArray alloc]init];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"Team" 
        inManagedObjectContext:self.genkDatabase.managedObjectContext]];
    request.predicate = [NSPredicate predicateWithFormat:@"position ==[c] %@",
                                                         @"Doelman"];
    NSLog(@"predicate is: %@",request.predicate);
    NSError *error = nil;
    NSLog(@"tot hier");
    results = [self.genkDatabase.managedObjectContext
               executeFetchRequest:request error:&error];

    return [results valueForKey:@"image"] ;

}

为了打开我的数据库,我使用以下方法。

-(void)viewWillAppear:(BOOL)animated
{
    if (!self.genkDatabase) {  // we'll create a default database if none is set
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        url = [url URLByAppendingPathComponent:@"Default appGenk Database"];
        self.genkDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; // setter will create this for us on disk
    }

}

- (void)setGenkDatabase:(UIManagedDocument *)genkDatabase
{
    if (_genkDatabase != genkDatabase) {
        _genkDatabase = genkDatabase;
        [self useDocument];
    }
}

- (void)useDocument
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.genkDatabase.fileURL path]]) {
        // does not exist on disk, so create it
        [self.genkDatabase saveToURL:self.genkDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            NSLog(@"bestaat niet");
            [self getTeam];


        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
        // exists on disk, but we need to open it
        [self.genkDatabase openWithCompletionHandler:^(BOOL success) {
            NSLog(@"closed");
            [self getTeam];
        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
        // already open and ready to use
        NSLog(@"Normal");
        [self getTeam];
    }

}

有谁能够帮助我?

亲切的问候。

EDTI

这很奇怪,因为当我在另一个视图控制器中测试该方法时它可以工作。唯一的区别是在我的 .h 文件中我这样做了。

 @interface MultimediaViewController : CoreDataTableViewController

在它不起作用的 viewController 中,它会这样做。

@interface MultimediaViewController : NRGridViewController

但我仍然像这样导入它。

#import "CoreDataTableViewController.h"
4

1 回答 1

0

好的,所以我尝试了另一个功能。它奏效了。我认为这可能与我的 managedobjectContext 有关。这是有效的功能。

- (NSArray *)getDoelmannen
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Team"];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sortOrder == 1"];
    [request setPredicate:predicate];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    NSError *error = nil;
    NSArray *mutableFetchResults = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
    if (mutableFetchResults == nil) {
        //handle error
    }else if ([mutableFetchResults count] == 0){
        NSLog(@"geen resultaten voor doelmannen");
    }else{
        //NSLog(@"Name: %@ , position %@",[mutableFetchResults valueForKey:@"name"],[mutableFetchResults valueForKey:@"position"]);
        return mutableFetchResults;
    }
    return mutableFetchResults;
}
于 2012-10-17T09:37:36.223 回答