Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (BOOL)importData {
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"dl" ofType:@"xml"];
- NSURL *localfile = [[NSURL alloc] initFileURLWithPath:filePath];
- [self mergeDataFromExternalSource:localfile];
- return TRUE;
- }
- - (BOOL)mergeDataFromExternalSource:(NSURL *)source {
- // Read in property list
- NSPropertyListFormat *format;
- NSString *errorDesc;
- NSData *plistXML = [[NSData alloc] initWithContentsOfURL:source];
- // Get entire XML file into an NSDictionary object which is ideal for plist data
- NSDictionary *data = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML
- mutabilityOption:NSPropertyListImmutable format:format errorDescription:&errorDesc];
- // Use NSMutableSet as we are parsing the plist and we only want one instance of each category
- // A set does that for us unlike an array.
- NSMutableSet *scategories = [[NSMutableSet alloc] init];
- // Get all category names into mutable array
- for(id object in data) {
- for(id key in object) {
- if([key isEqualToString:@"type"])
- [scategories addObject:(NSString *)[object objectForKey:key]];
- }
- }
- // However we cannot sort sets so we now change it into an array anyway
- NSMutableArray *categories = [[NSMutableArray alloc] initWithSet:scategories];
- [scategories release];
- [categories sortUsingSelector:@selector(compare:)];
- // Create fetch request to get all category names
- NSFetchRequest *categoriesRequest = [[[NSFetchRequest alloc] init] autorelease];
- [categoriesRequest setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:managedObjectContext_]];
- // Only fetch records that exist in our input data
- [categoriesRequest setPredicate:[NSPredicate predicateWithFormat:@"(name in %@)",categories]];
- // Sort our data store categories too so they match our input data.
- [categoriesRequest setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc]
- initWithKey:@"name" ascending:YES] autorelease]]];
- // Execute fetch
- NSError *error;
- NSArray *categoriesMatchingNames = [managedObjectContext_ executeFetchRequest:categoriesRequest error:&error];
- // We need to enumarate the categoriesMatchingNames array as we must control when next to fetch an object
- // We only want to fetch the next object when it matches a category as we have n input categories but potential n-x
- // stored ones.
- NSEnumerator *enumurateDSCategories = [categoriesMatchingNames objectEnumerator];
- // Get an initial category
- Category *dsCategory = (Category *)[enumateDSCategories nextObject];
- // Get the names to parse
- for(NSString *type in categories) {
- // If the data store category is blank (we had none or run out) or they don't match add it.
- if( (dsCategory == nil) || ![type isEqualToString:[dsCategory name]] ) {
- Category *category = (Category *)[NSEntityDescription insertNewObjectForEntityForName:@"Category" inManagedObjectContext:managedObjectContext_];
- [category setName:type];
- }
- // If the data store category matches the input one get the next data store category
- if( [type isEqualToString:[dsCategory name]] ) {
- Category *dsCategory = (Category *)[enumurateDSCategories nextObject];
- }
- }
- if(![managedObjectContext_ save:&error])
- {
- NSLog(@"Core data failed to save new objects!");
- }
- [categories release];
- [plistXML release];
- return TRUE;
- }
Add Comment
Please, Sign In to add comment