ios – NSFetchRequest找不到实体名称的NSEntityDescription

栏目: IOS · 发布时间: 5年前

内容简介:我已经尝试过提出类似问题的解决方案,但没有运气.获取请求无法找到实体名称“GarmentType”.这是我的数据模型:在此帮助程序类别的executeFetchRequest中抛出错误:

我已经尝试过提出类似问题的解决方案,但没有运气.获取请求无法找到实体名称“GarmentType”.

这是我的数据模型:

在此帮助程序类别的executeFetchRequest中抛出错误:

//
//  GarmentType+Create.m
//  Dressy
//
//  Created by Michael Mangold on 9/4/13.
//  Copyright (c) 2013 Michael Mangold. All rights reserved.
//

#import "GarmentType+Create.h"

@implementation GarmentType (Create)

// Creates or fetches a Core Data GarmentType entity.
+ (GarmentType *)garmentTypeWithName:(NSString *)name inManagedObjectContext:(NSManagedObjectContext *)context
{
    NSLog(@"name:%@ context:%@",name,context);
    GarmentType *garmentType = nil;

    // Build fetch request.
    NSFetchRequest *request          = [NSFetchRequest fetchRequestWithEntityName:@"GarmentType"];
    request.sortDescriptors          = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
    request.predicate                = [NSPredicate predicateWithFormat:@"name = %@",name];

    // Execute fetch requesst.
    NSError *error  = nil;
    NSArray *matches = [context executeFetchRequest:request error:&error]; // This is where it dies.

    if (!matches || [matches count] > 1) {
        NSLog(@"Error creating GarmentType.");
    } else if ([matches count] == 0) {
        garmentType          = [NSEntityDescription insertNewObjectForEntityForName:@"GarmentType" inManagedObjectContext:context];
        garmentType.name     = name;
    } else {
        garmentType = [matches lastObject];
    }

    return garmentType;
}

@end

名称记录正确的名称,上下文不为零.

当按下“完成”按钮时,从UITableViewController IBAction调用助手:

- (IBAction)handleDoneButton:(UIBarButtonItem *)sender
{
    // Add garment type to core data.
    [DressyHelper openDocumentUsingBlock:^(UIManagedDocument *document){
        GarmentType *newGarment = [GarmentType garmentTypeWithName:self.garmentTypeNameTextField.text inManagedObjectContext:document.managedObjectContext];
        NSLog(@"newGarment.name:%@",newGarment.name);
    }];
    [self dismissViewControllerAnimated:YES completion:nil];
}

newGarment.name记录正常.

这是GarmentType的.h:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Garment;

@interface GarmentType : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *garments;
@end

@interface GarmentType (CoreDataGeneratedAccessors)

- (void)addGarmentsObject:(Garment *)value;
- (void)removeGarmentsObject:(Garment *)value;
- (void)addGarments:(NSSet *)values;
- (void)removeGarments:(NSSet *)values;

@end

为代码转储道歉,但我一直在努力解决这个问题.更改实体名称会导致相同的错误,只是将新名称作为未找到的实体.

GarmentType检查员:

例外:

2013-12-10 13:51:50.320 Dressy[4336:70b] name:asdf context:<NSManagedObjectContext: 0x8ad86f0>
2013-12-10 13:51:50.323 Dressy[4336:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSFetchRequest could not locate an NSEntityDescription for entity name 'GarmentType''
*** First throw call stack:
(
    0   CoreFoundation                      0x017465e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014c98b6 objc_exception_throw + 44
    2   CoreData                            0x019b4b6e -[NSFetchRequest(_NSInternalMethods) _resolveEntityWithContext:] + 510
    3   CoreData                            0x019b34d6 -[NSManagedObjectContext executeFetchRequest:error:] + 70
    4   Dressy                              0x000051de +[GarmentType(Create) garmentTypeWithName:inManagedObjectContext:] + 510
    5   Dressy                              0x00002170 __54-[GarmentTypeAddTableViewController handleDoneButton:]_block_invoke + 208
    6   Dressy                              0x00006d51 __39+[DressyHelper openDocumentUsingBlock:]_block_invoke21 + 97
    7   UIKit                               0x00781f7b __59-[UIDocument saveToURL:forSaveOperation:completionHandler:]_block_invoke570 + 54
    8   libdispatch.dylib                   0x01e43440 _dispatch_barrier_sync_f_slow_invoke + 71
    9   libdispatch.dylib                   0x01e544b0 _dispatch_client_callout + 14
    10  libdispatch.dylib                   0x01e4275e _dispatch_main_queue_callback_4CF + 340
    11  CoreFoundation                      0x017aba5e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
    12  CoreFoundation                      0x016ec6bb __CFRunLoopRun + 1963
    13  CoreFoundation                      0x016ebac3 CFRunLoopRunSpecific + 467
    14  CoreFoundation                      0x016eb8db CFRunLoopRunInMode + 123
    15  GraphicsServices                    0x03a4d9e2 GSEventRunModal + 192
    16  GraphicsServices                    0x03a4d809 GSEventRun + 104
    17  UIKit                               0x00237d3b UIApplicationMain + 1225
    18  Dressy                              0x0000585d main + 141
    19  libdyld.dylib                       0x020e670d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

TIA


以上所述就是小编给大家介绍的《ios – NSFetchRequest找不到实体名称的NSEntityDescription》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java和Android开发学习指南(第二版)

Java和Android开发学习指南(第二版)

Budi Kurniawan / 李强 / 人民邮电出版社 / 2016-3 / 69.00元

本书是Java语言学习指南,特别针对使用Java进行Android应用程序开发展开了详细介绍。 全书共50章,分为两大部分。第1部分(第1章到第22章)主要介绍Java语言基础知识及其功能特性。第2部分(第23章到第50章)主要介绍如何有效地构建Android应用程序。 本书适合任何想要学习Java语言的读者阅读,特别适合想要成为Android应用程序开发人员的读者学习参考。一起来看看 《Java和Android开发学习指南(第二版)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具