无法从我的核心数据库中获取数据

时间:2012-10-17 07:26:17

标签: objective-c ios core-data nsfetchrequest

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

*** 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

这很奇怪,因为当我在anohter viewcontroller中测试方法时,它可以工作。唯一的区别在于我的.h文件,我这样做。

 @interface MultimediaViewController : CoreDataTableViewController

在viewController中,它无法正常工作。

@interface MultimediaViewController : NRGridViewController

但我仍然像这样导入它。

#import "CoreDataTableViewController.h"

1 个答案:

答案 0 :(得分: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;
}