[英]Core Data, can't save context
I'm simply trying to save a ManagedObjectContext but while I get no errors, the fetched request returns the object with none of the saved values. 我只是试图保存ManagedObjectContext,但是当我没有收到任何错误时,提取的请求将返回没有保存值的对象。 Consider this simple example.
考虑这个简单的例子。 You have a single object, you change a property and save it.
您只有一个对象,可以更改属性并保存。 The object is there but the property is not saved.
该对象在那里,但该属性未保存。 As you can see, I want only one object, and the fetch returns this one object.
如您所见,我只需要一个对象,而访存将返回该对象。 BTW, the code is in a simple class, not the app delegate or a view controller.
顺便说一句,代码在简单的类中,而不是应用程序委托或视图控制器。
Here is the sample code: 这是示例代码:
MyAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext* context = delegate.managedObjectContext;
NSEntityDescription *myEntityDesc = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:context];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:myEntityDesc];
NSError *error = nil;
NSArray *array = [context executeFetchRequest:request error:&error];
MyEntity* myEntity;
if (array == nil || [array count] < 1)
{
myEntity = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:context];
}
else
{
myEntity = [array objectAtIndex:0];
}
myEntity.BoolValue = [NSNumber numberWithBool:someBoolValue];
myEntity.ID = @"Some ID";
if ([context save:&error])
{
NSLog(@"no error");
}
else
{
NSLog([NSString stringWithFormat:@"found core data error: %@", [error localizedDescription]]);
}
Here's the code used to retrieve the values later: 以下是用于以后检索值的代码:
MyAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext* context = delegate.managedObjectContext;
NSEntityDescription *MyEntityDesc = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:context];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:MyEntityDesc];
NSError *error = nil;
NSArray *array = [context executeFetchRequest:request error:&error];
MyEntity* myEntity;
if (array == nil || [array count] < 1)
{
//handle error
}
else
{
myEntity = [array objectAtIndex:0];
}
return [myEntity.BoolValue boolValue];
What does your NSManagedObject
subclass look like? 您的
NSManagedObject
子类是什么样的? Since the fetch is working correctly (ie returning the entity), I suspect something is wrong in the subclass implementation. 由于提取工作正常(即返回实体),因此我怀疑子类实现中存在问题。
You should declare a @property
for each of the attributes on your data model. 您应该为数据模型上的每个属性声明一个
@property
。 And in the implementation file, instead of using @synthesize
you need to use @dynamic
. 并且在实现文件中,无需使用
@synthesize
,而需要使用@dynamic
。 Also make sure in your xcdatamodel
that the entity has its class set, as well as the name. 还要确保在您的
xcdatamodel
,该实体具有其类集以及名称。
@interface MyEntity : NSManagedObject
@property (nonatomic, strong) NSNumber * boolValue;
@end
@implementation MyEntity
@dynamic boolValue;
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.