iPhone - 应用程序启动时保存核心数据会导致多个同一对象
好吧,伙计们,我正在努力解决这个问题,但我遇到了困难。因此,这个应用程序有一个 XML 解析器,可以获取所有 xml,解析它们,并将所有数据存储在 Core Data 中。这一切都很好。然而,我试图保存核心数据并在下次运行时调用它,除非我这样做时解析器再次运行并且相同的项目再次聚合在 uitableview 中。我知道这是因为在 applicationDidFinishLaunchingWithOptions 中,每次运行应用程序时我都会调用 [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();
}
}
}
Okay guys, I am trying to get this figured out but I am having a difficult time. So this app has an XML parser that goes and grab all the xmls, parses them, and stores all the data in Core Data. This all works great. However I am trying to save the Core Data and call upon that the next time it is run, except when I do this the parser runs again and the same items are aggregated in the uitableview again. I know it is because in the applicationDidFinishLaunchingWithOptions I am calling the [parser getAllConferences] every time the application is run, however I am not sure how to only have this run when the Core Data is empty. Hopefully you all can shed some light on the matter :) Any and all comments and suggestions are welcome, if anything else is needed let me know!
- (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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有什么问题:
或者
注意上面的内容可能无法编译,但希望能够说明该方法。
What's wrong with:
Or
Note the above probably won't compile, but hopefully illustrates the approach.
您可以将最后一个项目与将从解析器中获取的项目进行比较。将解析器中的数据保存在 NSMutableArray 中,然后执行此检查查看具有相同项目的索引并停止检查聚合数据。
You can compare the last item you have with the items you are gonna take from the parser. Keep the data from parser in an NSMutableArray then do this checking see the index where you have the same item and stop checking aggregate your data.