5

我的 CoreData 数据库中有一个严重的问题。我在 2 个表之间创建了一对多关系,并在包含所有唯一值的表上添加了一个 Fetched 属性。所有代码在 iOS 5 中都按预期工作,但在 iOS 6 中我收到以下错误:

2013-02-08 15:15:49.382 ArdsBoroughCouncil [16152:c07] * 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<_NSObjectID_48_0 0xb25c3b0> valueForUndefinedKey:]:此类不符合键值编码关键tour_id。*First throw call stack: (0x31c7012 0x24a3e7e 0x324ffb1 0x1c445ed 0x1bb08db 0x1bb088d 0x1bceb17 0x1bfe746 0x21b74bb 0x21b7176 0x21b6e44 0x21b66f4 0x21b2d63 0x21b2a82 0x21b2850 0x21b2625 0x21b1e07 0x21b18f4 0x21b0a6d 0x21ae9c9 0x2201276 0x227b155 0x2201071 0x2880014 0x286fd5f 0x286faa3 0x220103b 0x2200e9e 0x21ae9c9 0x2209dfa 0x22375cf 0x2237a03 0x59de1 0x11c2753 0x11c2a7b 0x11d0590 0x11d85bd 0x11d8eab 0x11d94a3 0x11d9098 0x1534da3 0x1526ad9 0x1526b54 0x24b7705 0x10e2920 0x10e28b8 0x11a3671 0x11a3bcf 0x11a2d38 0x111233f 0x1112552 0x10f03aa 0x10e1cf8 0x34b3df9 0x34b3ad0 0x313cbf5 0x313c962 0x316dbb6 0x316cf44 0x316ce1b 0x34b27e3 0x34b2668 0x10df65c 0x291d 0x2845) libc++abi.dylib: terminate called throwing an exception

为 Fetched 属性定义的谓词是:

($FETCH_SOURCE.tour_id == tour_id) AND ($FETCH_SOURCE != SELF)

我已经定义了数组以返回所有接收我的 Tour 类中的所有值。

您是否知道 iOS 6 中 Fetched Properties 的任何问题?因为我无法理解这在 iOS 5 中没有问题,但在 iOS6 中失败。

请参阅下面的所有其他相关代码:

下面的代码是我如何检索所有记录:

- (void)loadRecordsFromCoreData {
    [self.managedObjectContext performBlockAndWait:^{
        [self.managedObjectContext reset];
        NSError *error = nil;
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:NSStringFromClass([Tour class]) inManagedObjectContext:self.managedObjectContext];
        [request setEntity:entityDescription];
        [request setResultType:NSManagedObjectResultType];
        [request setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"tour_id" ascending:YES]]];
        self.tours = [self.managedObjectContext executeFetchRequest:request error:&error];
    }];

下面的代码适用于我定义的 NSManagedObjects:

- 旅游

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

@class TourDetail;

@interface Tour : NSManagedObject

@property (nonatomic, retain) NSNumber * tour_id;
@property (nonatomic, retain) NSString * background_url;
@property (nonatomic, retain) NSString * summary;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSSet *tourdetails_tour;

@property (nonatomic, retain) NSArray *tourDetails;
@end

@interface Tour (CoreDataGeneratedAccessors)

- (void)addTourdetails_tourObject:(TourDetail *)value;
- (void)removeTourdetails_tourObject:(TourDetail *)value;
- (void)addTourdetails_tour:(NSSet *)values;
- (void)removeTourdetails_tour:(NSSet *)values;

@end
}
#import "Tour.h"
#import "TourDetail.h"


@implementation Tour

@dynamic tour_id;
@dynamic background_url;
@dynamic summary;
@dynamic title;
@dynamic tourdetails_tour;
@dynamic tourDetails;

@end

-- 旅游详情

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

@class Tour;

@interface TourDetail : NSManagedObject

@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSNumber * tour_id;
@property (nonatomic, retain) NSNumber * lattitude;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSString * audiofile;
@property (nonatomic, retain) NSString * detail;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) Tour *tour_tourdetails;

@end

#import "TourDetail.h"
#import "Tour.h"


@implementation TourDetail

@dynamic id;
@dynamic tour_id;
@dynamic lattitude;
@dynamic longitude;
@dynamic audiofile;
@dynamic detail;
@dynamic title;
@dynamic tour_tourdetails;

@end

您可以提供的任何建议将不胜感激:) 以及您可能拥有的关于如何最好地定义获取的属性的任何示例?

谢谢,迈克尔

4

1 回答 1

0

您在这里使用的代码不干净

- (void)loadRecordsFromCoreData {
    [self.managedObjectContext performBlockAndWait:^{
        [self.managedObjectContext reset];
        NSError *error = nil;
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:NSStringFromClass([Tour class]) inManagedObjectContext:self.managedObjectContext];
        [request setEntity:entityDescription];
        [request setResultType:NSManagedObjectResultType];
        [request setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"tour_id" ascending:YES]]];
        self.tours = [self.managedObjectContext executeFetchRequest:request error:&error];
    }];

事实上,这有点令人困惑。

您调用一个方法,该方法调用一个托管对象上下文,该上下文进一步在一个同步块内调用自身,该同步块无缘无故地重置自身,从而在块外设置一个属性“self.tours”。都很奇怪。你能解释一下你为什么这样做吗?

试试这个。

+(void)loadRecordsWithCompletion:(void(^)(NSArray *records, NSError *error))completion {
    NSManagedObjectContext *context; //Your context
    NSError *error;
    NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"attribute" ascending:YES];
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"EntityName"];
    [request setSortDescriptors:@[sortDesc]];
    NSArray *records = [context executeFetchRequest:request
                                              error:&error];
    completion(records, error);
}
于 2014-09-27T19:33:48.823 回答