2013-04-24 181 views
0

我在NSMutableArray中有9個Aviso元素,理論上我將這9個元素存儲在我的應用程序的核心數據中。問題是,當試圖從數據庫中檢索這些元素與NSFetchRequest時,我只得到8個空元素,最後一個元素正確顯示。核心數據:檢索數據庫對象時爲空數組

這是我的數據模型看起來像:

data model

而且說Aviso實體相關的說Aviso NSManagedObject

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

@interface Aviso : NSManagedObject{ 
    NSDate * date; 
    NSString * text; 
    NSString * idwarning; 
} 

@property (nonatomic, retain) NSDate * date; 
@property (nonatomic, retain) NSString * text; 
@property (nonatomic, retain) NSString * idwarning; 

@end 

在我WarningController I類第一負載的viewDidLoad我的警告:

for(Aviso * a in warnings) 
    { 
     [self saveObjectcdata:a]; 
    } 

    [self loadxcdata]; 

saveObjectcdata是:

-(void) saveObjectcdata:(Aviso *)aux 
{ 
    NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"Aviso" inManagedObjectContext:self.managedObjectContext]; 

    [object setValue:aux.idwarning forKey:@"idwarning"]; 
    [object setValue:aux.date forKey:@"date"]; 
    [object setValue:aux.text forKey:@"text"]; 

    [managedObjectContext insertObject:object]; 

    // Commit the change. 
    NSError *error = nil; 
    if (![managedObjectContext save:&error]) { 
     // Handle the error. 
    } 
    else{ 
     NSLog(@"Object saved: %@ in %@ and id: %@",aux.text,aux.date,aux.idwarning); 
    } 
} 

而且loadxcdata:

- (void) loadxcdata 
{ 
    [xcdataarray removeAllObjects]; 

    if (managedObjectContext == nil) // generar el contexto del CoreData 
    { 
     managedObjectContext = [(EmergencyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    } 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Aviso" inManagedObjectContext:managedObjectContext]; 
    [request setEntity:entity]; 

    // Execute the fetch -- create a mutable copy of the result. 
    NSError *error = nil; 
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
    if (mutableFetchResults == nil) { 
     // Handle the error. 
     NSLog(@"Error al recuperar:%@",error); 
    } 

    // Set self's events array to the mutable array, then clean up. 
    [self setXcdataarray:mutableFetchResults]; 

    for(Aviso * war in xcdataarray) 
    { 
     NSLog(@"Core data object: %@ in %@ and id: %@",war.text,war.date,war.idwarning); 
    } 

} 

這是我在我的控制檯我獲得。首先對象被妥善保存,但同時再次檢索它們,我只得到了最後一個:

2013-04-24 17:50:05.716 Emergencies[4286:907] CoreData: error: Failed to call designated initializer on NSManagedObject class 'Aviso' 
2013-04-24 17:50:05.844 Emergencies[4286:907] Object saved: sexto y ultimo in 2013-04-19 12:21:43 +0000 and id: 9 
2013-04-24 17:50:05.905 Emergencies[4286:907] Object saved: fifth later in 2013-04-18 11:17:50 +0000 and id: 8 
2013-04-24 17:50:05.961 Emergencies[4286:907] Object saved: fifth in 2013-04-18 11:17:44 +0000 and id: 7 
2013-04-24 17:50:06.014 Emergencies[4286:907] Object saved: forth later in 2013-04-18 11:11:57 +0000 and id: 6 
2013-04-24 17:50:06.071 Emergencies[4286:907] Object saved: cuarto in 2013-04-18 11:11:36 +0000 and id: 5 
2013-04-24 17:50:06.128 Emergencies[4286:907] Object saved: tercero in 2013-04-18 10:25:01 +0000 and id: 3 
2013-04-24 17:50:06.183 Emergencies[4286:907] Object saved: in 2013-04-18 10:57:27 +0000 and id: 4 
2013-04-24 17:50:06.237 Emergencies[4286:907] Object saved: rt in 2013-04-18 10:20:20 +0000 and id: 2 
2013-04-24 17:50:06.296 Emergencies[4286:907] Object saved: This is the first example in 2013-04-17 08:43:45 +0000 and id: 1 
2013-04-24 17:50:06.304 Emergencies[4286:907] Core data object: (null) in (null) and id: (null) 
2013-04-24 17:50:06.306 Emergencies[4286:907] Core data object: (null) in (null) and id: (null) 
2013-04-24 17:50:06.308 Emergencies[4286:907] Core data object: (null) in (null) and id: (null) 
2013-04-24 17:50:06.310 Emergencies[4286:907] Core data object: (null) in (null) and id: (null) 
2013-04-24 17:50:06.312 Emergencies[4286:907] Core data object: (null) in (null) and id: (null) 
2013-04-24 17:50:06.314 Emergencies[4286:907] Core data object: (null) in (null) and id: (null) 
2013-04-24 17:50:06.317 Emergencies[4286:907] Core data object: (null) in (null) and id: (null) 
2013-04-24 17:50:06.319 Emergencies[4286:907] Core data object: (null) in (null) and id: (null) 
2013-04-24 17:50:06.321 Emergencies[4286:907] Core data object: This is the first example in 2013-04-17 08:43:45 +0000 and id: 1 

編輯

我從核心數據實體創建一個新的類Aviso後(選擇實體然後編輯 - 在我saveObjectcdata方法創建新NSManagedObject,然後修改幾行,現在,它的工作原理

-(void) saveObjectcdata:(NSString *)warningId date:(NSDate*)date text:(NSString*)txt 
{ 
    Aviso *object = [NSEntityDescription insertNewObjectForEntityForName:@"Aviso" inManagedObjectContext:self.managedObjectContext]; 

    object.idwarning = warningId; 
    object.date = date; 
    object.text = txt; 

    // Commit the change. 
    NSError *error = nil; 
    if (![managedObjectContext save:&error]) { 
     // Handle the error. 
    } 
    else{ 
     NSLog(@"Object saved: %@ in %@ and id: %@",object.text,object.date,object.idwarning); 
    } 
} 
+0

不知道的手,但我敢打賭,它是與該錯誤在日誌的第一行 - '無法調用NSManagedObject類'Aviso'上的指定初始值設定項。另外,'saveObjectcdata:'方法是我見過的最奇怪,最不必要的事情之一。 – 2013-04-24 17:17:25

回答

1

我明白爲什麼你。空值,我不明白爲什麼最後一個對象保存成功。

您不按照設計運行的方式使用CoreData。

1)您不必實現實體的內部數據結構。如果您喜歡一個類來表示您的實體,請在設計中選擇您的實體,然後選擇:編輯器 - >創建NSManagedObject子類。
除非需要附加功能,否則無需添加任何內容。
有關更多詳細信息,請勿覆蓋不應超出範圍的內容see here

2)您以錯誤的方式添加新對象。
要插入一個項目,你只能打電話:[NSEntityDescription insertNewObjectForEntityForName:<EntityName> inManagedObjectContext:<context>]
您可以將結果轉換爲所需的託管對象子類。

saveObjectdata:應該是這個樣子:

-(void) saveObjectcdata:(id)warningId date:(NSDate*)date text:(NSString*)text 
{ 
    Avisio *object = [NSEntityDescription insertNewObjectForEntityForName:@"Aviso" inManagedObjectContext:self.managedObjectContext]; 

    object.idwarning = warningId; 
    object.date = date; 
    object.text = text; 

    // Commit the change. 
    NSError *error = nil; 
    if (![managedObjectContext save:&error]) { 
     // Handle the error. 
    } 
    else{ 
     NSLog(@"Object saved: %@ in %@ and id: %@",object.text,object.date,object.idwarning); 
    } 
} 

3)我建議你閱讀this得到的東西

+0

謝謝丹!我剛剛解決了我的問題。問題是我在我的Aviso類中使用合成,而不是動態的,並且我正在調用兩次插入方法。 – civiac 2013-04-25 11:02:18