新项目和旧核心数据模型

时间:2016-10-01 09:24:24

标签: ios objective-c core-data

我确实有一个带有Core Data模型的现有项目。我正在尝试运行一个新项目(在旧模型之上)并阅读模型本身的内容。

现在,该应用程序在前一个应用程序之上运行顺畅。我导入了.xcdatamodeld,没有任何东西崩溃。但是,当我尝试从实体中获取一些应该有一些的值时(因为我之前从旧项目中加载了它),我得到0作为结果。

我是Core Data的新手,所以我可能会遗漏一些非常简单的东西。

这是我目前的persistentStoreCoordinator(我在这里找到:Coredata after Application Update):

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Model"];
    NSManagedObjectModel *managedObjectModel = [self managedObjectModel];
    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];

    // Check if we already have a persistent store
    if ( [[NSFileManager defaultManager] fileExistsAtPath: [storeURL path]] ) {
        NSLog(@"We have a persistent store!");
        NSDictionary *existingPersistentStoreMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType: NSSQLiteStoreType URL: storeURL error: &error];
        if ( !existingPersistentStoreMetadata ) {
            // Something *really* bad has happened to the persistent store
            //[NSException raise: NSInternalInconsistencyException format: @"Failed to read metadata for persistent store %@: %@", storeURL, error];
            NSLog(@"Failed to read metadata for persistent store %@: %@", storeURL, error);
        }

        if ( ![managedObjectModel isConfiguration: nil compatibleWithStoreMetadata: existingPersistentStoreMetadata] ) {
            if ( ![[NSFileManager defaultManager] removeItemAtURL: storeURL error: &error] )
                NSLog(@"*** Could not delete persistent store, %@", error);
        } // else the existing persistent store is compatible with the current model - nice!
        NSLog(@"Existing store is compatible!");
    } // else no database file yet

    [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                              configuration:nil
                                                        URL:storeURL
                                                    options:nil
                                                      error:&error];
    return _persistentStoreCoordinator;
}

我得到了日志“我们有一个持久存储”和“现有存储是兼容的”,所以我假设我实际上找到了保存在设备上的核心数据模型。

这是我的managedObjectModel:

- (NSManagedObjectModel *)managedObjectModel {
    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.

    if (_managedObjectModel != nil) {
        NSLog(@"Not Null!");

        return _managedObjectModel;
    }
   NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    return _managedObjectModel;
}

这就是我试图读取数据的方式(我在didFinishLaunchingWithOptions中调用方法):

-(void) readData {

    NSManagedObjectContext *context = [self managedObjectContext];

    if(context != nil)
        NSLog(@"not null");

    NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@ "StaffAccount"];

    NSError* error = nil;
    NSArray* results = [context executeFetchRequest:request error:&error];

    if (!results || error) { // nil is an error
        // handle error
        NSLog(@"Errore!");

    }

    NSArray *arr = [context executeFetchRequest:request error: &error];

    NSInteger count = [arr count];

    NSLog(@"%ld", (long)count);
}

0 个答案:

没有答案