1

さて、要点をまっすぐに...

マギアレコードを使用するのはこれが初めてですマギアパンダからhttp://yannickloriot.com/2012/03/magicalrecord-how-to-make-programming-with-core-data-pleasant/からtutroialをフォローしました

これが私のコードです

Person.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})"
)

なぜ常にデータ障害なのですか?それは魔法少女の問題ですか?または私のコードに何か問題がありますか?

4

2 に答える 2

7

これはエラーではありません。「フォルト」と呼ばれる Core Data の機能です。Appleの説明は次のとおりです。

フォルトは、アプリケーションが消費するメモリの量を減らします。フォルトは、まだ完全には実現されていない管理対象オブジェクトを表すプレースホルダー オブジェクト、または関係を表すコレクション オブジェクトです。

管理対象オブジェクトのフォルトは適切なクラスのインスタンスですが、その永続変数はまだ初期化されていません。リレーションシップ フォールトは、リレーションシップを表すコレクション クラスのサブクラスです。フォルトにより、Core Data はオブジェクト グラフに境界を設定できます。障害が認識されないため、管理対象オブジェクトの障害が消費するメモリが少なくなり、障害に関連する管理対象オブジェクトをメモリに表示する必要がまったくなくなります。

各 Person オブジェクトを表示したい場合は、それらに明確にアクセスする必要があります。

于 2012-11-25T08:52:00.120 に答える
1

マジカルレコでもしょっちゅう失敗してたし。たくさんのヘッドバンギングの後。

[[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {

if (!success) {

NSLog(@"::: Error saving context :::");

}

}];

今、あなたがfindAllをするとき。デバッガーで出力してオブジェクトを確認します。

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

これにより、結果がデバッガーに出力されます。

于 2015-05-11T09:45:08.383 に答える