简体   繁体   中英

iPhone - Saving Core Data results in multiple of the same object when app started up

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM