2013-02-08 56 views
5

我的CoreData數據庫中存在嚴重問題。我創建了2個表之間的一對多關係,並在包含所有唯一值的表上添加了一個Fetched屬性。所有代碼工作中的iOS 5,但在iOS 6中我收到以下errror預期:獲取的物業在iOS5但不是iOS6的作品

2013年2月8日15:15:49.382 ArdsBoroughCouncil [16152:C07] *終止 應用程序由於未捕獲的異常' NSUnknownKeyException',原因: '[<_NSObjectID_48_0 0xb25c3b0> 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 : 終止叫做拋出異常

對取出屬性定義的謂詞是:

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

我已經定義了該數組以返回所有在我的Tour類中接收的所有值。

您是否意識到iOS 6的獲取屬性有問題?因爲我無法理解這在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我已經定義:

- Tour

#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 

- Tour Detail

#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 

任何建議您可以提供將不勝感激:),以及你可能必須爲如何最好地定義獲取屬性的例子?

感謝, 邁克爾

+0

剛出於好奇,你是如何填充數據庫在第一個地方,並使用IOS 5模擬器? – 2013-02-08 17:57:50

+0

嗨空間粉塵我填充數據庫使用mms字典對象,然後我堅持使用模型。這是使用iOS5模擬器完成的,但即使在使用ios6模擬器時數據仍然存在,但是當我嘗試檢索獲取的屬性時,只會使用IOS6引發異常。你有什麼想法可以解決這個問題嗎? – 2013-02-10 23:33:39

+0

嗨邁克爾,你找到了解決辦法嗎?我有同樣的問題。 – epologee 2013-02-27 15:38:34

回答

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); 
} 
相關問題