Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 核心数据:检索数据库对象时为空数组_Ios_Database_Core Data_Persistence_Nsmanagedobjectcontext - Fatal编程技术网

Ios 核心数据:检索数据库对象时为空数组

Ios 核心数据:检索数据库对象时为空数组,ios,database,core-data,persistence,nsmanagedobjectcontext,Ios,Database,Core Data,Persistence,Nsmanagedobjectcontext,我在NSMutableArray中有九个Aviso元素,理论上我将这九个元素存储在我的应用程序的核心数据中。问题是,当尝试使用NSFetchRequest从数据库检索这些元素时,我只得到八个空元素,最后一个元素显示正确 我的数据模型是这样的: Aviso实体与AvisoNSManagedObject相关: #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @interface Aviso :

我在
NSMutableArray
中有九个Aviso元素,理论上我将这九个元素存储在我的应用程序的核心数据中。问题是,当尝试使用
NSFetchRequest
从数据库检索这些元素时,我只得到八个空元素,最后一个元素显示正确

我的数据模型是这样的:

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
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);
    }
}
和加载数据:

- (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
编辑

在我从核心数据实体创建了一个新类之后(选择该实体,然后选择编辑器-创建新的
NSManagedObject
,然后在我的
saveObjectcdata
方法中修改几行,现在它可以工作了

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

我理解为什么会得到空值,我不理解为什么最后一个对象会成功保存。

您没有按照设计的方式使用CoreData。

1) 您不必实现实体的内部数据结构。如果您想要一个类来表示您的实体,请在designed中选择您的实体,然后选择:Editor->Create NSManagedObject子类。
除非您需要其他功能,否则无需添加任何内容。
不要覆盖不应覆盖的内容以获取更多详细信息。

2) 您正在以错误的方式添加新对象。
要插入项目,您只需调用:
[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:

您可以将结果强制转换为所需的托管对象子类。

您的
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) 我建议您阅读以掌握情况

不确定,但我打赌这与日志第一行的错误有关--
未能调用NSManagedObject类“Aviso”上的指定初始值设定项。另外,这个
saveObjectcdata:`方法是我见过的最奇怪、最不必要的事情之一。谢谢Dan!我刚刚解决了我的问题。问题是我在Aviso类中使用了Synthesis而不是dynamic,而且我调用了两次insert方法。
-(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);
    }
}