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

iPhone-当应用程序启动时,保存核心数据会导致多个相同的对象,iphone,core-data,ios4,loading,save,Iphone,Core Data,Ios4,Loading,Save,好了,伙计们,我正试图弄明白这一点,但我有一个困难的时间。因此,这个应用程序有一个XML解析器,它可以获取所有XML,解析它们,并将所有数据存储在核心数据中。这一切都很好。但是,我试图保存核心数据,并在下次运行时调用它,除非我这样做,否则解析器将再次运行,相同的项将再次聚合到uitableview中。我知道这是因为在ApplicationIDFinishLaunchingWithOptions中,每次运行应用程序时,我都会调用[parser getAllConferences],但是我不确定如何

好了,伙计们,我正试图弄明白这一点,但我有一个困难的时间。因此,这个应用程序有一个XML解析器,它可以获取所有XML,解析它们,并将所有数据存储在核心数据中。这一切都很好。但是,我试图保存核心数据,并在下次运行时调用它,除非我这样做,否则解析器将再次运行,相同的项将再次聚合到uitableview中。我知道这是因为在ApplicationIDFinishLaunchingWithOptions中,每次运行应用程序时,我都会调用[parser getAllConferences],但是我不确定如何仅在核心数据为空时运行此程序。希望大家能对此事有所了解:)欢迎提出任何意见和建议,如果还需要什么,请告诉我

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

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

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

有什么问题吗:

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

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

有什么问题:

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


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

您知道我可以从Apple文档或堆栈溢出中引用的代码片段吗?请检查此处以创建一个数组:然后创建一个for循环,直到for循环中的数组大小使用一个if子句检查返回的值是否相同。尝试编写一些代码,如果您仍然有问题,请发送代码,我们可以讨论。您知道我可以从Apple Docs或on stack overflow中引用的代码片段吗?请检查此处以创建数组:然后创建一个for循环,直到for循环中的数组大小使用一个if子句检查何时返回相同的值。试着写一些代码,如果你仍然有问题发送代码,我们可以讨论。