为什么NSFetchRequest的类错了? [英] Why is the class wrong for NSFetchRequest?

查看:262
本文介绍了为什么NSFetchRequest的类错了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用未公开的API(Osirix),我有一个姐妹问题到一个我张贴在这里。



我无法从受管对象上下文中加载对象。



从API加载,使用_context和_model

的实例

  2010-05-28 14:05:13.588 OsiriX [44012 :a0f] Entity:Study 
2010-05-28 14:05:13.589 OsiriX [44012:a0f] EntityClassName:DicomStudy
2010-05-28 14:05:13.589 OsiriX [44012:a0f] ClassName :DicomStudy

从Fetch请求中加载(以及我自己的_context和_model实例)

  2010-05-28 14:19:09.956 rcOsirix [44431:7a03]实体:Study 
2010-05-28 14 :19:09.957 rcOsirix [44431:7a03] EntityClassName:DicomStudy
2010-05-28 14:19:09.958 rcOsirix [44431:7a03] ClassName:NSManagedObject

输出:

  NSLog(@Entity:% @,[[item entity] name]); 
NSLog(@EntityClassName:%@,[[item entity] managedObjectClassName]);
NSLog(@ClassName:%s,class_getName(object_getClass([item class])));

因此很明显,即使Entity认为它是一个DicomSeries - 它只是一个NSManagedObject。 DicomSeries有一些硬编码KVC的东西,我遇到了一个问题在我的另一个问题。



我在这个线程中追求一个不同的推理线 -

 <$ c $ 



c> - (NSManagedObjectModel *)managedObjectModel
{
if(managedObjectModel)返回managedObjectModel;

NSMutableSet * allBundles = [[NSMutableSet alloc] init];
[allBundles addObject:[NSBundle mainBundle]];
[allBundles addObjectsFromArray:[NSBundle allFrameworks]];

managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@/ OsiriXDB_DataModel.mom]]];
[allBundles release];

return managedObjectModel;
}

- (NSManagedObjectContext *)managedObjectContextLoadIfNecessary:(BOOL)loadIfNecessary
{
NSError * error = nil;
NSString * localizedDescription;
NSFileManager * fileManager;

if(currentDatabasePath == nil)
return nil;

if(managedObjectContext)
return managedObjectContext;

if(loadIfNecessary == NO)return nil;

fileManager = [NSFileManager defaultManager];

[persistentStoreCoordinator release];

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];

NSURL * url = [NSURL fileURLWithPath:currentDatabasePath];

if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:& error])
{
NSLog(@***** ***** managedObjectContextLoadIfNecessary FAILED:%@,error);
localizedDescription = [error localizedDescription];
error = [NSError errorWithDomain:@OsiriXDomaincode:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error,NSUnderlyingErrorKey,[NSString stringWithFormat:@Store Configuration Failure:%@,((localizedDescription!= nil)?localizedDescription :@Unknown Error)],NSLocalizedDescriptionKey,nil]];
}

[[managedObjectContext undoManager] setLevelsOfUndo:1];
[[managedObjectContext undoManager] disableUndoRegistration];

//这行非常重要,如果没有database.sql文件
[self saveDatabase:currentDatabasePath];

return managedObjectContext;
}

这是我的代码:

  NSManagedObjectModel * DataModule :: managedObjectModel()
{
if(_managedObjectModel)return _managedObjectModel;

NSMutableSet * allBundles = [[NSMutableSet alloc] init];
[allBundles addObject:[NSBundle mainBundle]];
[allBundles addObjectsFromArray:[NSBundle allFrameworks]];

_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@/ OsiriXDB_DataModel.mom]]];

[allBundles release];

return [_managedObjectModel retain];
}

...
NSError * error = nil;
[_storeCoordinator release];

_storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel()];

_context = [[NSManagedObjectContext alloc] init];
[_context setPersistentStoreCoordinator:_storeCoordinator];

NSURL * url = [NSURL fileURLWithPath:[[NSString alloc] initWithCString:_DBPath.c_str()]];

if(url == nil){[pool release]; _loadLock = false; return nil; }

if(![_ storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:& error])
{
NSLog ****** managedObjectContextLoadIfNecessary FAILED:%@,error);
NSString * localizedDescription = [error localizedDescription];
error = [NSError errorWithDomain:@OsiriXDomaincode:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error,NSUnderlyingErrorKey,[NSString stringWithFormat:@Store Configuration Failure:%@,((localizedDescription!= nil)?localizedDescription :@Unknown Error)],NSLocalizedDescriptionKey,nil]];

//退出失败
[pool release]; _loadLock = false; return nil;
}

[[_context undoManager] setLevelsOfUndo:1];
[[_context undoManager] disableUndoRegistration];
...

我包含所有相同的框架....但 _allBundles 甚至不用于创建 managedObjectModel ,所以我不知道它应该做什么,除非将它们加载到内存以便 mom 在载入时可以查看它们。



总共丢失。



帮助!



为什么我的FetchRequest返回的对象具有相同的实体出现 NSManagedObjects 而不是 DicomStudy s?我包括DicomStudy.h,所以它应该在创建模型,上下文和获取请求期间看到对象。

 请求setEntity:[[managedObjectModel()entitiesByName] objectForKey:@Study]]; 

提前感谢,


解决方案

Hooookay,



我感觉像一个巨大的我的每一个问题,但哦。



因此,答案是我所包含的框架没有编译源代码。 BenT在 Apple开发论坛上提到,源代码需要编译。 ..所以我查看框架我正在导入它似乎只是复制头文件,,不编译任何东西。



这样做(我认为,因为我没有从框架的开发人员的答复),因为该框架旨在用作主要软件的插件的一部分。由于插件架构加载所有编译的类,只有框架中的头阻止了 objc [1378]:类BLAH在X和Y中实现。将使用两个中的一个。



因此,看起来我需要包含源代码或make一个新的框架。



感谢 Marcus 帮助我。我希望这不只是一个RTFM问题,因为我只有从2009年11月开始为苹果平台开发。我还没有学习CoreData ... ...



-Stephen


I am working with an undocumented API (Osirix) and I have a sister-question to the one I posted here.

I am having trouble loading objects from a managed object context.

With loading from API, using their instance of _context and _model

2010-05-28 14:05:13.588 OsiriX[44012:a0f] Entity: Study
2010-05-28 14:05:13.589 OsiriX[44012:a0f] EntityClassName: DicomStudy
2010-05-28 14:05:13.589 OsiriX[44012:a0f] ClassName: DicomStudy

With loading from Fetch Request (and my own instance of _context, and _model)

2010-05-28 14:19:09.956 rcOsirix[44431:7a03] Entity: Study
2010-05-28 14:19:09.957 rcOsirix[44431:7a03] EntityClassName: DicomStudy
2010-05-28 14:19:09.958 rcOsirix[44431:7a03] ClassName: NSManagedObject

output given by:

NSLog(@"Entity: %@",[[item entity] name]);
NSLog(@"EntityClassName: %@", [[item entity] managedObjectClassName]);
NSLog(@"ClassName: %s", class_getName(object_getClass([item class])));

So it is obvious that even though the Entity thinks it is a DicomSeries - it is not. It is just a NSManagedObject. DicomSeries has some "hard-coded" KVC stuff that I ran into a problem with in my other question.

I'm pursuing a different line of reasoning in this thread - with the loading of the objects.

The following is their code:

- (NSManagedObjectModel *)managedObjectModel
{
    if (managedObjectModel) return managedObjectModel;

    NSMutableSet *allBundles = [[NSMutableSet alloc] init];
    [allBundles addObject: [NSBundle mainBundle]];
    [allBundles addObjectsFromArray: [NSBundle allFrameworks]];

    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: [NSURL fileURLWithPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/OsiriXDB_DataModel.mom"]]];
    [allBundles release];

    return managedObjectModel;
}

- (NSManagedObjectContext *) managedObjectContextLoadIfNecessary:(BOOL) loadIfNecessary
{
    NSError *error = nil;
    NSString *localizedDescription;
    NSFileManager *fileManager;

    if( currentDatabasePath == nil)
        return nil;

    if (managedObjectContext)
        return managedObjectContext;

    if( loadIfNecessary == NO) return nil;

    fileManager = [NSFileManager defaultManager];

    [persistentStoreCoordinator release];

    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: self.managedObjectModel];

    managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator: persistentStoreCoordinator];

    NSURL *url = [NSURL fileURLWithPath: currentDatabasePath];

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error])
    {
        NSLog(@"********** managedObjectContextLoadIfNecessary FAILED: %@", error);
        localizedDescription = [error localizedDescription];
        error = [NSError errorWithDomain:@"OsiriXDomain" code:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error, NSUnderlyingErrorKey, [NSString stringWithFormat:@"Store Configuration Failure: %@", ((localizedDescription != nil) ? localizedDescription : @"Unknown Error")], NSLocalizedDescriptionKey, nil]];
    }

    [[managedObjectContext undoManager] setLevelsOfUndo: 1];
    [[managedObjectContext undoManager] disableUndoRegistration];

    // This line is very important, if there is NO database.sql file
    [self saveDatabase: currentDatabasePath];

    return managedObjectContext;
}

This is my code:

NSManagedObjectModel* DataModule::managedObjectModel()
{
if (_managedObjectModel) return _managedObjectModel;

    NSMutableSet *allBundles = [[NSMutableSet alloc] init];
    [allBundles addObject: [NSBundle mainBundle]];
    [allBundles addObjectsFromArray: [NSBundle allFrameworks]];

_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: [NSURL fileURLWithPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/OsiriXDB_DataModel.mom"]]];

    [allBundles release];

return [_managedObjectModel retain];
}

...
        NSError *error = nil;
        [_storeCoordinator release];

        _storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel()];

        _context = [[NSManagedObjectContext alloc] init];
        [_context setPersistentStoreCoordinator: _storeCoordinator];

        NSURL *url = [NSURL fileURLWithPath: [[NSString alloc] initWithCString:_DBPath.c_str()]];

        if (url == nil) { [pool release]; _loadLock = false; return nil; }

        if (![_storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error])
        {
            NSLog(@"********** managedObjectContextLoadIfNecessary FAILED: %@", error);
            NSString *localizedDescription = [error localizedDescription];
            error = [NSError errorWithDomain:@"OsiriXDomain" code:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error, NSUnderlyingErrorKey, [NSString stringWithFormat:@"Store Configuration Failure: %@", ((localizedDescription != nil) ? localizedDescription : @"Unknown Error")], NSLocalizedDescriptionKey, nil]];

            //Exit Failure
            [pool release]; _loadLock = false; return nil;
        }

        [[_context undoManager] setLevelsOfUndo: 1];
        [[_context undoManager] disableUndoRegistration];
...

I am including all the same frameworks.... but _allBundles isn't even used to create the managedObjectModel so I don't know what it's supposed to do except load them into memory so that the mom can look at them while loading.

Totally lost.

Help!

Why would objects returned by my FetchRequest with the same Entity come out as NSManagedObjects and not DicomStudys? I'm including DicomStudy.h so it should see the object during creation of the model, context, and fetch request.

[request setEntity: [[managedObjectModel() entitiesByName] objectForKey:@"Study"]];

Thanks in advance,

-Stephen

解决方案

Hooookay,

I feel like a gargantuan turd for answering every single one of my questions, but oh well.

So, the answer is that the Framework I was including had no compiled source. BenT over on the Apple Dev Forums mentioned that the source needed to be compiled... so I looked into the Framework I was importing at it appears that it only copies the header files, and does not compile anything.

This was done (I think, since I've gotten no reply from the dev's of that Framework) because the framework was intended to be used as part of a plug-in for the main software. Since the plug-in architecture loads all the compiled classes, having just the headers in the Framework prevented the objc[1378]: Class BLAH is implemented in both X and Y. One of the two will be used. Which one is undefined. "fun-time" errors.

So, it looks like I'll have to either include the source or make a new framework from their source.

Thanks to Marcus for helping me out. I was hoping this wasn't just a "RTFM" problem since I've only ben dev'ing for the Apple platform since Nov of 2009. I hadn't gotten around to learning CoreData yet...

-Stephen

这篇关于为什么NSFetchRequest的类错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆