失去理智导致获取核心数据总是返回错误



好的,直截了当

...

这是我第一次使用魔法熊猫的魔法记录我从 http://yannickloriot.com/2012/03/magicalrecord-how-to-make-programming-with-core-data-pleasant/那里追随了Tutroial

这是我的代码

人.h

@property (nonatomic, retain) NSString * firstname;
@property (nonatomic, retain) NSString * lastname;
@property (nonatomic, retain) NSNumber * age;

AppDelegate.M

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     [MagicalRecord setupCoreDataStackWithStoreNamed:@"MyDatabase.sqlite"];
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[JASidePanelController alloc] init];
    self.viewController.shouldDelegateAutorotateToVisiblePanel = NO;
  //  self.viewController = [[[ViewControllerCenter alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.viewController.centerPanel = [[UINavigationController alloc] initWithRootViewController:[[ViewControllerCenter alloc] init]];
    //UINavigationController *navContorller=[[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.viewController.rightPanel = [[NetraRightWindow alloc] init];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
     [MagicalRecord cleanUp];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end

这是我保存的方法:

-(void)fetchRecords{
   NSManagedObjectContext *localContext= [NSManagedObjectContext MR_defaultContext];
    // Create a new Person in the current thread context
    Person *person                          = [Person MR_createInContext:    [NSManagedObjectContext MR_defaultContext]];
    person.firstname                        = @"Test";
    person.lastname                         = @"merdeka123";
    person.age=[NSNumber numberWithInt:123];
    [[NSManagedObjectContext MR_defaultContext] MR_save];
    NSArray *Result=[Person MR_findAll];
    NSLog(@"Result==%@",Result);

结果是:

2012-11-25 15:43:38.033 Trip[10491:15e03] Cok==(
    "<Person: 0x866fe40> (entity: Person; id: 0x866e170 <x-coredata://15583C03-3EB4-479B-9EF2-B0BC750FC987/Person/p1> ; data: <fault>)",
    "<Person: 0x866fe80> (entity: Person; id: 0x8670210 <x-coredata://15583C03-3EB4-479B-9EF2-B0BC750FC987/Person/p2> ; data: <fault>)",
    "<Person: 0x81471a0> (entity: Person; id: 0x815c2a0 <x-coredata://15583C03-3EB4-479B-9EF2-B0BC750FC987/Person/p3> ; data: {n    age = 123;n    firstname = Test;n    lastname = merdeka123;n    time = nil;n})"
)

为什么它总是数据故障? 这是魔法记录中的问题吗? 还是我的代码中有任何错误?

这不是错误 - 这是Core Data的一个功能,称为"错误"。以下是苹果的描述:

错误会减少应用程序消耗的内存量。一个 fault 是一个占位符对象,它表示一个托管对象 尚未完全实现,或集合对象 表示关系:

托管对象错误是相应类的实例,但 其持久变量尚未初始化。关系错误 是集合类的子类,表示 关系。故障允许核心数据在 对象图。由于未意识到错误,因此托管对象错误 消耗更少的内存,并且与故障相关的托管对象不是 完全需要在内存中表示。

如果要查看每个 Person 对象,则必须专门访问它们。

我也一直在魔法唱片中出错。经过多次头部撞击。

[[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"::: Error saving context :::");
}
}];

现在,当您找到全部时,通过在调试器中打印来检查对象。

Person *testObject = [Result objectAtIndex:0];
NSLog(@"%@", testObject.firstname);

这应该在调试器中打印结果。

最新更新