【问题标题】:Fetched Property works in iOS5 but not iOS6获取的属性适用于 iOS5 但不适用于 iOS6
【发布时间】:2013-01-24 10:20:09
【问题描述】:

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

2013-02-08 15:15:49.382 ArdsBoroughCouncil[16152:c07] * 终止 应用程序由于未捕获的异常“NSUnknownKeyException”,原因: '[<_nsobjectid_48_0> valueForUndefinedKey:]: 这个类是 不符合键 tour_id 的键值编码。 * 第一次抛出调用栈:(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: 终止称为抛出异常

为 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

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

谢谢, 迈克尔

【问题讨论】:

  • 只是出于好奇,您最初是如何填充数据库的,以及使用什么 IOS 5 模拟器?
  • Hi Space Dust 我使用 mms 字典对象填充了数据库,然后使用模型进行了持久化。这是使用 iOS5 模拟器完成的,但即使我使用 ios6 模拟器,数据也会保留,但是当我尝试检索我获取的属性时,只有 IOS6 才会引发异常。你有什么想法我可以解决这个问题吗?
  • 嗨迈克尔,你找到解决办法了吗?我也有同样的问题。
  • 这太奇怪了,我只有在 iOS 6 模拟器上运行的 new 项目才有这个。它不会发生在 5.x 上,也不会发生在旧项目中甚至 iOS 6 模拟器中。很奇怪。
  • 对我来说听起来像是对版本控制问题建模。在这种情况下,我尝试的第一步是增加模型版本。

标签: ios ios5 core-data ios6 ios6.1


【解决方案1】:

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

- (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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    相关资源
    最近更新 更多