怪异的核心数据反应 – 不检测NSManagedObject子类
在我使用Core Data
应用程序中似乎有一个非常奇怪的错误。
我已经处理了所有需要的东西,并且像最近几周一样工作。
我昨天closures了项目,今天开始开发( 没有修改代码 ),现在每当我想添加一行到我的数据库,我得到一个exception,说下面的类不是NSManagedObject
的子NSManagedObject
,我可笑地肯定是不正确的,首先是因为它工作了几天,第二,因为我自动生成NSManagedObject
子类。 我尝试使用Editor->Create NSManagedObject Subclasses
在我的数据模型中Editor->Create NSManagedObject Subclasses
重新生成类,但它根本没有帮助我。
这真是奇怪,我得到这个例外蓝色突然没有甚至在我的代码修改行:(
有人有什么想法? 我真的很感激任何帮助。 谢谢
顺便说一句,这里有一些代码可以帮助:
错误 :
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '"Message" is not a subclass of NSManagedObject.
消息类别头文件(自动生成):
#import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @class File; @interface Message : NSManagedObject @property (nonatomic, retain) NSString * data; @property (nonatomic, retain) NSDate * date; @property (nonatomic, retain) NSString * fileId; @property (nonatomic, retain) NSString * number; @property (nonatomic, retain) NSString * status; @property (nonatomic, retain) NSString * type; @property (nonatomic, retain) NSString * xmppId; @property (nonatomic, retain) File *contains; @end
我试图创build一个新的消息对象的方法:
+ (void)sendXMPPMessage:(NSString *)messageStr containingFile:(NSData *)fileData toNumber:(NSString *)number { if(fileData == nil) { NSString *newID = [self generateNewXMPPMessageID]; SunBirdAppDelegate *appDelegate = (SunBirdAppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate sendXMPPMessage:messageStr withID:newID toNumber:number]; NSMutableDictionary *messageDataDic = [[NSMutableDictionary alloc] init]; [messageDataDic setObject:messageStr forKey:@"body"]; [messageDataDic setObject:@"Outgoing" forKey:@"type"]; [messageDataDic setObject:newID forKey:@"xmppId"]; [messageDataDic setObject:@"Sending" forKey:@"status"]; [messageDataDic setObject:[NSDate date] forKey:@"date"]; [messageDataDic setObject:number forKey:@"number"]; [Message messageWithMessageInfo:messageDataDic inManagedObjectContext:[appDelegate managedObjectContext]]; } else { //Handle Database With File Data } }
创build一个新的消息对象 – >在消息类上创build类别(Message + Create.m) :
+ (Message *)messageWithMessageInfo:(NSDictionary *)messageInfo inManagedObjectContext:(NSManagedObjectContext *)context { SunBirdAppDelegate *appDelegate = (SunBirdAppDelegate *)[[UIApplication sharedApplication] delegate]; Message *newMessage = nil; NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Message"]; request.predicate = [NSPredicate predicateWithFormat:@"xmppId = %@", [messageInfo objectForKey:@"msgId"]]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"xmppId" ascending:YES]; request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSError *error = nil; NSArray *matches = [context executeFetchRequest:request error:&error]; if (!matches || ([matches count] > 1)) { // handle error } else if ([matches count] == 0) { newMessage = [NSEntityDescription insertNewObjectForEntityForName:@"Message" inManagedObjectContext:context]; newMessage.data = [messageInfo objectForKey:@"body"]; newMessage.type = [messageInfo objectForKey:@"type"]; newMessage.date = [messageInfo objectForKey:@"date"]; newMessage.xmppId = [messageInfo objectForKey:@"xmppId"]; newMessage.status = [messageInfo objectForKey:@"status"]; newMessage.number = [messageInfo objectForKey:@"number"]; if ( [[messageInfo allKeys] containsObject:@"fileId"] ) { newMessage.fileId = [messageInfo objectForKey:@"fileId"]; newMessage.contains = [messageInfo objectForKey:@"fileData"]; } else { newMessage.fileId = NULL; newMessage.contains = NULL; } } else { newMessage = [matches lastObject]; NSLog(@"WARNING : NO NEW MESSAGE STORED IN DB ! MESSAGE WITH SAME XMPPID RETURNED"); } [appDelegate saveContext]; return newMessage; }
这是我被告知Message
不是NSManagedObject
的子NSManagedObject
:
newMessage = [NSEntityDescription insertNewObjectForEntityForName:@"Message" inManagedObjectContext:context];
这是我在一个我工作的项目中偶然发现的错误。
Message
不能用于实体名称…为什么? … 苹果 … ???
真的不知道。
重命名为别的东西,这通常可以解决问题。