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

发布于 2024-11-14 19:14:38 字数 1803 浏览 6 评论 0原文

好吧,伙计们,我正在努力解决这个问题,但我遇到了困难。因此,这个应用程序有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趁年轻赶紧闹 2024-11-21 19:14:38

有什么问题:

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
}

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

What's wrong with:

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

Or

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

Note the above probably won't compile, but hopefully illustrates the approach.

岁月静好 2024-11-21 19:14:38

您可以将最后一个项目与将从解析器中获取的项目进行比较。将解析器中的数据保存在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文