Guest User

Untitled

a guest
Jul 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. - (BOOL)importData {
  2. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"dl" ofType:@"xml"];
  3. NSURL *localfile = [[NSURL alloc] initFileURLWithPath:filePath];
  4. [self mergeDataFromExternalSource:localfile];
  5. return TRUE;
  6. }
  7.  
  8. - (BOOL)mergeDataFromExternalSource:(NSURL *)source {
  9. // Read in property list
  10. NSPropertyListFormat *format;
  11. NSString *errorDesc;
  12. NSData *plistXML = [[NSData alloc] initWithContentsOfURL:source];
  13.  
  14. // Get entire XML file into an NSDictionary object which is ideal for plist data
  15. NSDictionary *data = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML
  16. mutabilityOption:NSPropertyListImmutable format:format errorDescription:&errorDesc];
  17.  
  18. // Use NSMutableSet as we are parsing the plist and we only want one instance of each category
  19. // A set does that for us unlike an array.
  20. NSMutableSet *scategories = [[NSMutableSet alloc] init];
  21.  
  22. // Get all category names into mutable array
  23. for(id object in data) {
  24. for(id key in object) {
  25. if([key isEqualToString:@"type"])
  26. [scategories addObject:(NSString *)[object objectForKey:key]];
  27. }
  28. }
  29.  
  30. // However we cannot sort sets so we now change it into an array anyway
  31. NSMutableArray *categories = [[NSMutableArray alloc] initWithSet:scategories];
  32. [scategories release];
  33. [categories sortUsingSelector:@selector(compare:)];
  34.  
  35. // Create fetch request to get all category names
  36. NSFetchRequest *categoriesRequest = [[[NSFetchRequest alloc] init] autorelease];
  37. [categoriesRequest setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:managedObjectContext_]];
  38. // Only fetch records that exist in our input data
  39. [categoriesRequest setPredicate:[NSPredicate predicateWithFormat:@"(name in %@)",categories]];
  40.  
  41. // Sort our data store categories too so they match our input data.
  42. [categoriesRequest setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc]
  43. initWithKey:@"name" ascending:YES] autorelease]]];
  44.  
  45. // Execute fetch
  46. NSError *error;
  47. NSArray *categoriesMatchingNames = [managedObjectContext_ executeFetchRequest:categoriesRequest error:&error];
  48.  
  49. // We need to enumarate the categoriesMatchingNames array as we must control when next to fetch an object
  50. // We only want to fetch the next object when it matches a category as we have n input categories but potential n-x
  51. // stored ones.
  52. NSEnumerator *enumurateDSCategories = [categoriesMatchingNames objectEnumerator];
  53.  
  54. // Get an initial category
  55. Category *dsCategory = (Category *)[enumateDSCategories nextObject];
  56. // Get the names to parse
  57. for(NSString *type in categories) {
  58. // If the data store category is blank (we had none or run out) or they don't match add it.
  59. if( (dsCategory == nil) || ![type isEqualToString:[dsCategory name]] ) {
  60. Category *category = (Category *)[NSEntityDescription insertNewObjectForEntityForName:@"Category" inManagedObjectContext:managedObjectContext_];
  61. [category setName:type];
  62. }
  63. // If the data store category matches the input one get the next data store category
  64. if( [type isEqualToString:[dsCategory name]] ) {
  65. Category *dsCategory = (Category *)[enumurateDSCategories nextObject];
  66. }
  67. }
  68.  
  69. if(![managedObjectContext_ save:&error])
  70. {
  71. NSLog(@"Core data failed to save new objects!");
  72. }
  73.  
  74. [categories release];
  75. [plistXML release];
  76.  
  77. return TRUE;
  78. }
Add Comment
Please, Sign In to add comment