Skip to content

Instantly share code, notes, and snippets.

@quique123
Created March 9, 2013 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quique123/5126202 to your computer and use it in GitHub Desktop.
Save quique123/5126202 to your computer and use it in GitHub Desktop.
{
results = (
{
createdAt = "2012-07-09T07:13:24.593Z";
details = "First Store";
image = {
"__type" = File;
name = "9d2d8a0d-36fb-4abe-9908-bebd7fb39056-christmas.gif";
url = "http://files.serverapp.com/bcee5dd3-46dc-40a8-abe6-37da2732e809/1store.png";
};
name = MainSt;
objectId = SVrYV4gGH;
services = (
DriveIn,
DineIn
);
updatedAt = "2013-01-01T02:24:18.097Z";
}
);
}
-(void)parsePlistIntoCD{
// 1: Now put the plistDictionary into CD...create get ManagedObjectContext
NSManagedObjectContext *context = self.managedObjectContext;
NSError *error;
// 2: Create Request & set Entity for request
NSFetchRequest *holidayRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *topicEntityDescription = [NSEntityDescription entityForName:@"Holiday" inManagedObjectContext:context];
[holidayRequest setEntity:topicEntityDescription];
// 3: Create new NSManagedObject
//Holiday *holidayObjectToSeed = nil;
NSManagedObject *newHoliday = nil;
// 4: Execute fetch just to make sure?
NSArray *holidayFetchedArray = [context executeFetchRequest:holidayRequest error:&error];
if (error) NSLog(@"Error encountered in executing topic fetch request: %@", error);
// 5: No holidays in database so we proceed to populate the database
if ([holidayFetchedArray count] == 0) {
//Get path to plist file
NSString *holidaysPath = [[NSBundle mainBundle] pathForResource:@"PreloadedFarsiman" ofType:@"plist"];
//Put data into an array (with dictionaries in it)
NSArray *holidayDataArray = [[NSArray alloc] initWithContentsOfFile:holidaysPath];
//Get number of items in that array
int numberOfTopics = [holidayDataArray count];
//Loop thru array items...
for (int i = 0; i<numberOfTopics; i++) {
//get each dict at each node
NSDictionary *holidayDataDictionary = [holidayDataArray objectAtIndex:i];
//Insert new object
newHoliday = [NSEntityDescription insertNewObjectForEntityForName:@"Holiday" inManagedObjectContext:context];
//Parse all keys in each dict object
[newHoliday setValuesForKeysWithDictionary:holidayDataDictionary];
//Save and or log error
[context save:&error];
if (error) NSLog(@"Error encountered in saving topic entity, %d, %@, Hint: check that the structure of the pList matches Core Data: %@",i, newHoliday, error);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment