1

Core Data に複数回アクセスするたびにエラーが発生します。データを受信して​​表示しようとしているテーブルビューがあります。

SecondViewController.h に変数があります。

#import <UIKit/UIKit.h>
#import "CooperTestModel.h"
#import "Event.h"
#import <CoreData/CoreData.h>

@interface SecondViewController : UITableViewController {
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *eventArray;
}

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSMutableArray *eventArray;

- (void) fetchRecords;

@end

および SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController
@synthesize managedObjectContext, eventArray;

私の AppDelegate では、ViewController に設定されたオブジェクトを取得します。

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    SecondViewController *tableController = [[SecondViewController alloc] init];
    tableController.managedObjectContext = [self managedObjectContext];

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];

    [window addSubview: [self.navigationController view]];
    [window makeKeyAndVisible];

}


- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        NSLog(@"managedObjectContext already in use. Returning instance.");
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }

    NSLog(@"managedObjectContext nil. Returning new instance.");
    return managedObjectContext;
}

次に、tableView でデータを取得しています。

- (void)fetchRecords {

    NSLog(@"Fetching records.");
    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];
    // Define how we will sort the records
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:sortDescriptors];
    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (!mutableFetchResults) {
        NSLog(@"Error fetching data.");
    }
    // Save our fetched data to an array
    [self setEventArray: mutableFetchResults];

    NSLog(@"Records found: %i", [eventArray count]);
}

これは最初の試行では問題なく機能しますが、その後、次のエラーが発生します。

2013-09-07 19:55:03.715 CooperTest[2697:11603] managedObjectContext nil. Returning new instance.
2013-09-07 19:55:03.729 CooperTest[2697:11603] Fetching records.
2013-09-07 19:55:03.732 CooperTest[2697:11603] Records found: 3
2013-09-07 19:55:03.734 CooperTest[2697:11603] Rows in table: 3
2013-09-07 19:55:04.874 CooperTest[2697:11603] Fetching records.
2013-09-07 19:55:04.878 CooperTest[2697:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Event''
libc++abi.dylib: terminate called throwing an exception

これに関するアイデアはありますか?新しいプロジェクトをセットアップせず、「コア データを使用する」オプションにチェックを入れました。そこで、CoreData.framework とコードを AppDelegate に追加しました。また、モデルとクラスは正しいです (最後に 3 つのレコードを保存できましたfetchRecords)。

4

1 に答える 1

2

最後に、使用して修正しました

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
managedObjectContext = appDelegate.managedObjectContext;

AppDelegate から渡す代わりに、ViewControlelr で。

于 2013-09-07T19:56:53.347 に答える