【发布时间】:2011-07-06 14:14:08
【问题描述】:
我正在开发一个核心数据项目,该项目将与我的 Ipad 通信并来回发送核心数据。我已经大致弄清楚了网络部分,但是我在获取简单的获取请求时遇到了问题。这是获取请求:
NSManagedObjectContext *context=[[[NSDocumentController sharedDocumentController] currentDocument] managedObjectContext];
//NSManagedObjectContext *context=[self managedObjectContext];
if (context == nil){
NSLog(@"Crap");
}
NSLog(@"Context: %@",context);
//fetch request: (found here: http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Articles/05_Fetching.html)
NSLog(@"Starting to fetch:");
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Song" inManagedObjectContext:context];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"cueNo" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
如果我使用这个:
NSManagedObjectContext *context=[[[NSDocumentController sharedDocumentController] currentDocument] managedObjectContext];
然后我收到一条错误消息:
+entityForName: 找不到实体名称“Song”的 NSManagedObjectModel
如果我使用这个:
NSManagedObjectContext *context=[self managedObjectContext];
即使我在实体 Song 中有数据,mutableFetchResults 也会返回 null。
我使用第一种方法直接从以前的版本复制了这个获取请求来设置上下文,它在那里工作得很好。
任何帮助将不胜感激!
疑难解答:
Printing description of persistentStoreCoordinator:
Printing description of _managedObjectModel:
(<NSManagedObjectModel: 0x1001c5250>) isEditable 0, entities {
Song = "(<NSEntityDescription: 0x100149ba0>) name Song, managedObjectClassName NSManagedObject, renamingIdentifier Song, isAbstract 0, superentity name (null), properties {\n cueName = \"(<NSAttributeDescription: 0x1001c5600>), name cueName, isOptional 1, isTransient 0, entity Song, renamingIdentifier cueName, validation predicates (\\n), warnings (\\n), versionHashModifier (null), attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n cueNo = \"(<NSAttributeDescription: 0x1001c5570>), name cueNo, isOptional 1, isTransient 0, entity Song, renamingIdentifier cueNo, validation predicates (\\n), warnings (\\n), versionHashModifier (null), attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null)";
}, fetch request templates {
newFetchRequest = "<NSFetchRequest: 0x1001c5420> (entity: Song; predicate: (cueNo < \"0\"); sortDescriptors: (null); limit: 0)";
}
Printing description of _managedObjectContext:
<NSManagedObjectContext: 0x1001c5890>
【问题讨论】:
标签: objective-c core-data