iPhone - 应用程序启动时保存核心数据会导致多个相同的对象

Posted

技术标签:

【中文标题】iPhone - 应用程序启动时保存核心数据会导致多个相同的对象【英文标题】:iPhone - Saving Core Data results in multiple of the same object when app started up 【发布时间】:2011-06-11 21:29:12 【问题描述】:

好的,伙计们,我正在努力解决这个问题,但我遇到了困难。所以这个应用程序有一个 XML 解析器,它可以抓取所有的 xml,解析它们,并将所有数据存储在 Core Data 中。这一切都很好。但是,我正在尝试保存核心数据并在下次运行时调用它,除非我这样做时解析器再次运行并且相同的项目再次聚合在 uitableview 中。我知道这是因为在 applicationDidFinishLaunchingWithOptions 中我每次运行应用程序时都会调用 [parser getAllConferences],但是我不确定如何仅在核心数据为空时运行此操作。希望大家能对此事有所了解:)欢迎任何和所有的 cmets 和建议,如果需要其他任何内容,请告诉我!

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    


        SHPEXmlParser *parser = [SHPEXmlParser alloc];

        [parser initWithManagedObjectContext:[self managedObjectContext]];

        [parser getAllConferences];
        [parser release];

        [self.viewController initWithContext:[self managedObjectContext]];

        // Override point for customization after application launch.
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
    

- (void)applicationWillTerminate:(UIApplication *)application

    [self saveContext];


- (void)saveContext

    NSError *error = nil;
    if (managedObjectContext != nil)
    
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
        
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
         
    

【问题讨论】:

【参考方案1】:

有什么问题:

if (NO == [[NSFileManager defaultManager] fileExistsAtPath:@"put your path to sqlite file here"]) 
    //parse stuff and load data into store

或者

NSFetchRequest *conferencesRequest = [NSFetchRequest fetchRequestForEntityWithName:@"conferences" inManagedObjectContext:context];
NSArray *conferences = [self.managedObjectContext executeFetchRequest:conferencesRequest];
if (conferences.count == 0) 
       //parse stuff and load data into store

请注意,上述内容可能无法编译,但希望能说明该方法。

【讨论】:

【参考方案2】:

您可以将您拥有的最后一项与您将从解析器中获取的项进行比较。将解析器中的数据保存在 NSMutableArray 中,然后执行此检查,查看具有相同项目的索引并停止检查聚合数据。

【讨论】:

你知道我可以从 Apple Docs 或堆栈溢出中引用的代码 sn-p 吗? 检查此处以创建一个数组:developer.apple.com/library/mac/#documentation/Cocoa/Reference/… 然后创建一个 for 循环,直到 for 循环内的数组大小使用一个 if 子句检查何时返回相同的值。尝试写一些代码,如果仍然有问题发送代码,我们可以讨论。

以上是关于iPhone - 应用程序启动时保存核心数据会导致多个相同的对象的主要内容,如果未能解决你的问题,请参考以下文章

核心数据导致 iPhone 崩溃

重新启动应用程序时数据丢失(核心数据)

当我启动 iphone 模拟器时,Lion Osx 有时会导致所有应用程序崩溃

核心数据:保存结束时冻结

在 Core Data iPhone 应用程序的视图层次结构中保存位置

将 mogenerator 与 Core Data 实体一起使用会导致保存数据存储时出错