nil不是合法的NSManagedObjectContext参数,用于搜索实体名称'DPickup''

时间:2015-07-17 06:28:07

标签: ios objective-c xcode core-data uisplitviewcontroller

我正在尝试做什么:获取一个SplitViewController,用于两个相互传递数据的视图控制器之间。我很新,所以请提供任何例子。

错误: 使用线程1:信号SIGBRT

Scan[29101:2468584] Unknown class MainTableView in Interface Builder file.2015-07-17 02:06:59.356 X-Scan[29101:2468584] Registering for keyboard events2015-07-17 02:08:16.944 X-Scan[29101:2468584] Unregister for keyboard events2015-07-17 02:08:16.947 X-Scan[29101:2468584] Unknown class MainTableViewin Interface Builder file.2015-07-17 02:08:16.951 X-Scan[29101:2468584] Registering for keyboard events2015-07-17 02:08:17.769 X-Scan[29101:2468584] Unregister for keyboard events

我认为主要关注的是

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

我尝试了什么: 各种堆栈溢出解决方案,但似乎没有影响它的工作。我已经尝试了 prepareForSegue:sender ,但没有运气。我也尝试过使用delegate和managedObjectContext类,但也没有运气。 (当您尝试调试多年没有文档的代码时很难)。

我的Appdelegate.h代码

#import <UIKit/UIKit.h>
#import "X_ScanAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([X_ScanAppDelegate class]));
    }
}

Appdelgate.m

#import "X_ScanAppDelegate.h"

@implementation X_ScanAppDelegate


@synthesize window = _window;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;



// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"mom"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}
- (NSManagedObjectContext *)managedObjectContext {

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

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

    if (coordinator != nil) {
        NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

        [moc performBlockAndWait:^{
            [moc setPersistentStoreCoordinator: coordinator];
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(mergeChangesFrom_iCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator];
        }];
        _managedObjectContext = moc;
    }

    return _managedObjectContext;
}

- (void)mergeChangesFrom_iCloud:(NSNotification *)notification {

    NSLog(@"Merging in changes from iCloud...");

    NSManagedObjectContext* moc = [self managedObjectContext];

    [moc performBlock:^{

        [moc mergeChangesFromContextDidSaveNotification:notification];

        NSNotification* refreshNotification = [NSNotification notificationWithName:@"SomethingChanged"
                                                                            object:self
                                                                          userInfo:[notification userInfo]];

        [[NSNotificationCenter defaultCenter] postNotification:refreshNotification];
    }];
}

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if((_persistentStoreCoordinator != nil)) {
        return _persistentStoreCoordinator;
    }

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    NSPersistentStoreCoordinator *psc = _persistentStoreCoordinator;

    // Set up iCloud in another thread:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // ** Note: if you adapt this code for your own use, you MUST change this variable:
        NSString *iCloudEnabledAppID = @"rentxent";

        // ** Note: if you adapt this code for your own use, you should change this variable:
        NSString *dataFileName = @"X-Scan.sqlite";

        /*
         NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"X-Scan.sqlite"];*/


        // ** Note: For basic usage you shouldn't need to change anything else

        NSString *iCloudDataDirectoryName = @"Data.nosync";
        NSString *iCloudLogsDirectoryName = @"Logs";
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *localStore = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:dataFileName];
        NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];

        if (iCloud) {

            NSLog(@"iCloud is working");

            NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];

            NSLog(@"iCloudEnabledAppID = %@",iCloudEnabledAppID);
            NSLog(@"dataFileName = %@", dataFileName);
            NSLog(@"iCloudDataDirectoryName = %@", iCloudDataDirectoryName);
            NSLog(@"iCloudLogsDirectoryName = %@", iCloudLogsDirectoryName);
            NSLog(@"iCloud = %@", iCloud);
            NSLog(@"iCloudLogsPath = %@", iCloudLogsPath);

            if([fileManager fileExistsAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]] == NO) {
                NSError *fileSystemError;
                [fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]
                       withIntermediateDirectories:YES
                                        attributes:nil
                                             error:&fileSystemError];
                if(fileSystemError != nil) {
                    NSLog(@"Error creating database directory %@", fileSystemError);
                }
            }

            NSString *iCloudData = [[[iCloud path]
                                     stringByAppendingPathComponent:iCloudDataDirectoryName]
                                    stringByAppendingPathComponent:dataFileName];

            NSLog(@"iCloudData = %@", iCloudData);

            NSMutableDictionary *options = [NSMutableDictionary dictionary];
            [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
            [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
            [options setObject:iCloudEnabledAppID            forKey:NSPersistentStoreUbiquitousContentNameKey];
            [options setObject:iCloudLogsPath                forKey:NSPersistentStoreUbiquitousContentURLKey];

            [psc lock];

            [psc addPersistentStoreWithType:NSSQLiteStoreType
                              configuration:nil
                                        URL:[NSURL fileURLWithPath:iCloudData]
                                    options:options
                                      error:nil];

            [psc unlock];
        }
        else {
            NSLog(@"iCloud is NOT working - using a local store");
            NSMutableDictionary *options = [NSMutableDictionary dictionary];
            [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
            [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];

            [psc lock];

            [psc addPersistentStoreWithType:NSSQLiteStoreType
                              configuration:nil
                                        URL:localStore
                                    options:options
                                      error:nil];
            [psc unlock];

        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"SomethingChanged" object:self userInfo:nil];
        });
    });

    return _persistentStoreCoordinator;
    /*

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

     NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"X-Scan.sqlite"];

     NSError *error = nil;
     _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
     if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {


     NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
     abort();
     }

     return _persistentStoreCoordinator;
     */
}

#pragma mark - Application's Documents directory

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

- (void)saveContext{

}

@end

我正在尝试查看的视图控制器头文件

@interface SecondViewController : UIViewController {

    UITableView *tblPickups;
    UIBarButtonItem *btnClosedPickups;
    UIBarButtonItem *btnAllPickups;
    UIBarButtonItem *btnOpenPickups;
    NSMutableArray * PickupArray;
    NSMutableArray * PickupArrayAll;
    UISearchBar *searchBar;
}



@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

@property (nonatomic, strong) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) IBOutlet UITableView *tblPickups;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *btnClosedPickups;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *btnAllPickups;
- (IBAction)btnAllPickups_Clicked:(id)sender;
- (IBAction)btnOpenPickups_Clicked:(id)sender;
- (IBAction)btnClosedPickups_Clicked:(id)sender;

@property (nonatomic, strong) IBOutlet UIBarButtonItem *btnOpenPickups;

我的视图控制器主文件

#pragma mark TableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    EditPickupView *firstViewController =[self.storyboard instantiateViewControllerWithIdentifier:@"EditPickupView"];
    //[[EditPickupView alloc] initWithNibName:@"EditPickupView" bundle:nil];
    DPickup * thepickup=[PickupArray objectAtIndex:indexPath.row];


    firstViewController.thePickup=thepickup;

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Update Pickups" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;

    //[scanItemViewController setIsEditing:NO];
    //[scanItemViewController setIsListBased:NO];
    [self.navigationController pushViewController:firstViewController animated:YES];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int rows=[PickupArray count];

    return rows;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Set up the cell

    int therow=indexPath.row;
    DPickup * currentItem= [PickupArray objectAtIndex:therow];


    cell.textLabel.text=[NSString stringWithFormat:@"%@-%@-%@",currentItem.orderNumber,currentItem.clientCompany,currentItem.clientName];



    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {


    return UITableViewCellEditingStyleDelete;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    // Updates the appearance of the Edit|Done button as necessary.

    [super setEditing:editing animated:animated];
    [tblPickups setEditing:editing animated:animated];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //AppData *appdata=[AppData sharedInstance];
    [tableView beginUpdates];
    AppData *app=[AppData sharedInstance];
    NSManagedObjectContext *context=[app managedObjectContext];

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        DPickup *pitem=[PickupArray objectAtIndex:indexPath.row];

        for(DPickup *itm in PickupArrayAll){
            if(itm==pitem){
                [PickupArrayAll removeObject:itm];
                [PickupArray removeObject:itm];
                break;
            }
        }

        [context deleteObject:pitem];
        [context save:nil];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];

        /*
        DPickup *pitem=[PickupArray objectAtIndex:indexPath.row];
        AppData* appdata=[AppData sharedInstance];
        [appdata deletePickupItemsForPickup:[pitem getID]];
        [appdata deletePickup:[pitem getID]];

        [PickupArray removeObjectAtIndex:indexPath.row];
        PickupArrayAll=[appdata readPickups];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
         */
    }

    [tableView endUpdates];
}
#pragma mark LifeCycle Functions


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{

    id delegate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [delegate managedObjectContext];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.title=@"Update Pickup";
    btnAllPickups.style=UIBarButtonItemStyleDone;
    btnClosedPickups.style=UIBarButtonItemStyleBordered;
    btnOpenPickups.style=UIBarButtonItemStyleBordered;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadFetchedResults:)
                                                 name:@"SomethingChanged"
                                               object:[[UIApplication sharedApplication] delegate]];

    [super viewDidLoad];
}
- (void)visewDidUnload {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload
{
    [self setTblPickups:nil];
    [self setSearchBar:nil];
    [self setBtnClosedPickups:nil];
    [self setBtnOpenPickups:nil];
    [self setBtnAllPickups:nil];
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self];


    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void) viewDidAppear:(BOOL)animated {
    AppData *appData=[AppData sharedInstance];

    NSManagedObjectContext *context=[appData managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"DPickup" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error;



    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];




    PickupArray=[[NSMutableArray alloc] initWithArray:fetchedObjects];

    PickupArrayAll=[[NSMutableArray alloc] initWithArray:PickupArray];

    //Pickups== [appData readPickups];
    [tblPickups reloadData];

}
- (IBAction)btnSynchClicked:(id)sender {
    AppData *app=[AppData sharedInstance];
    [app.managedObjectContext mergeChangesFromContextDidSaveNotification:nil];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [PickupArray removeAllObjects];
    for(DPickup* item in PickupArrayAll){
        if([ [item.orderNumber uppercaseString] hasPrefix:[searchText uppercaseString]] || [searchText isEqualToString:@""])
            [PickupArray addObject:item];
    }
    [tblPickups setScrollsToTop:YES];
    [tblPickups reloadData];
}

- (IBAction)btnAllPickups_Clicked:(id)sender {
    [PickupArray removeAllObjects];
    for(DPickup* item in PickupArrayAll){
            [PickupArray addObject:item];
    }
    [tblPickups setScrollsToTop:YES];
    [tblPickups reloadData];

    btnAllPickups.style=UIBarButtonItemStyleDone;
    btnClosedPickups.style=UIBarButtonItemStyleBordered;
    btnOpenPickups.style=UIBarButtonItemStyleBordered;
}

- (IBAction)btnOpenPickups_Clicked:(id)sender {
    [PickupArray removeAllObjects];
    for(DPickup* item in PickupArrayAll){
        if([item.isPickedUp intValue]==0)
            [PickupArray addObject:item];
    }
    [tblPickups setScrollsToTop:YES];
    [tblPickups reloadData];    

    btnAllPickups.style=UIBarButtonItemStyleBordered;
    btnClosedPickups.style=UIBarButtonItemStyleBordered;
    btnOpenPickups.style=UIBarButtonItemStyleDone;
}

- (IBAction)btnClosedPickups_Clicked:(id)sender {
    [PickupArray removeAllObjects];
    for(DPickup* item in PickupArrayAll){
        if([item.isPickedUp intValue]==1)
            [PickupArray addObject:item];
    }
    [tblPickups setScrollsToTop:YES];
    [tblPickups reloadData]; 
    btnAllPickups.style=UIBarButtonItemStyleBordered;
    btnClosedPickups.style=UIBarButtonItemStyleDone;
    btnOpenPickups.style=UIBarButtonItemStyleBordered;
}

- (void)reloadFetchedResults:(NSNotification*)note {
    NSLog(@"Underlying data changed ... refreshing!");
    AppData *appData=[AppData sharedInstance];

    NSManagedObjectContext *context=[appData managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"DPickup" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error;



    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];




    PickupArray=[[NSMutableArray alloc] initWithArray:fetchedObjects];

    PickupArrayAll=[[NSMutableArray alloc] initWithArray:PickupArray];

    //Pickups== [appData readPickups];
    [tblPickups reloadData];

}

@end

我很新,所以如果需要任何示例或更多信息,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于您尝试在nil上下文中获取特定类型(DPickup)的所有值。

此行很可能将上下文设置为nil

NSManagedObjectContext *context=[appData managedObjectContext]

您确定已正确设置AppData单身人士吗?