好吧,伙计们,我正在努力解决这个问题,但我遇到了困难。因此,这个应用程序有一个XML解析器,它可以获取所有XML,解析它们,并将所有数据存储在核心数据中。这一切都很好。然而,我正在尝试保存核心数据,并在下次运行时调用它,除非我这样做时解析器再次运行,并且相同的项目再次聚集在uitableview中。我知道这是因为在应用程序DidFinishLaunchingWithOptions中,每次运行应用程序时,我都会调用[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();
}
}
}
有什么问题
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
}
请注意,上面的内容可能不会编译,但希望能说明这种方法。
您可以将最后一个项目与要从解析器中获取的项目进行比较。将解析器中的数据保存在NSMutableArray中,然后进行此检查—查看具有相同项的索引,并停止检查聚合数据。