Tag Archive: Core-Data


There are some interesting challenges with incorporating iCloud support, especially into an existing application. One, which I’ve been working through, is that you can no longer copy in a pre-existing store for the purposes of loading data. Such a move appears to work but does not upload anything to the cloud, the cloud store will only see future changes and not have the initial data set.
The solution to this is to use NSPersistentStoreCordinator’s migratePersistentStore:toURL:options:withType:error method. However I found the documentation somewhat lacking in clarity so here is the solution that now appears to be working for me.

There are some things I wanted to guarantee, which are probably not applicable to many so I’ll document them here:

- The application has gone through a few object model changes and I wanted to support people that may have skipped versions.
- The application’s data store location has changed on one occasion, when I moved the store out of Documents and into the Library directory due to adding iTunes document sharing.

Before we look at the code we’ll have a look at a rough outline of the procedure, in handy bullet points, step is 4 is the one that took some figuring out:

  1. Run everything following this asynchronously.
  2. Determine location of any existing stores, if not default to initial data set.
  3. Check to see if application supports iCloud, e.g. is running iOS 5.
  4. Configure store options if it does support iCloud.
  5. Lock the persistent store coordinator.
  6. If there is a cloud data set already, simply add it.
  7. If there isn’t DON’T add it, the migrate function adds it.
  8. Unlock persistent store coordinator.
  9. Send messages out so that view controllers that need to can redo their fetches.

A few caveats about the following, it’s not tested aside from ensuring it does migrate data into the store. I’ve not yet verified that the data then careers across the cloud to another device but I’ll update the post as soon as I do.

The code isn’t optimal, but that should be obvious! ;) The method should be called where in your code you’d normally add the store. A lot of the framework, minus the migrations stuff, is based on some example iCloud code.
Also note that the if block with the “//Migrate old store to new AND migrate into cloud” comment is devoid of code. In that block I would need to update the old store to match the new one and THEN migrate the store as I’m using my own migrations code which also allows importing of new data and a few other tweaks, which I think I’ve previously blogged.

CLOUD_NAME,STORE_NAME are #defines. DLog is a #define macro to NSLog that is blank in production environments.

- (void)aSynchronouslyAddPersistentStore {
  NSString *cloudPath = [[self applicationLibraryDirectory] stringByAppendingPathComponent:CLOUD_NAME];
  NSString *preCloudPath = [[self applicationLibraryDirectory] stringByAppendingPathComponent:STORE_NAME];
  NSString *defaultStorePath = [[NSBundle mainBundle]
                                pathForResource:@"InitialData" ofType:@"sqlite"];
  NSString *oldStorePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:STORE_NAME];

  // do this asynchronously since if this is the first time this particular device is syncing with preexisting
  // iCloud content it may take a long long time to download
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *storeUrl = [NSURL fileURLWithPath:cloudPath];
    NSURL *migrateUrl = nil;

    /**
     Find out which store has the data we need to migrate,
     if any.
     */
    if (![fileManager fileExistsAtPath:cloudPath]) {
      if ([fileManager fileExistsAtPath:preCloudPath]) {
        // Migrate old application data.
        migrateUrl = [NSURL fileURLWithPath:preCloudPath];
      } else if ([fileManager fileExistsAtPath:oldStorePath]) {
        // Migrate old store to new AND migrate into cloud.
      } else if ([fileManager fileExistsAtPath:defaultStorePath]) {
        // Migrate (copy) in initial recipe data set.
        migrateUrl = [NSURL fileURLWithPath:defaultStorePath];
      }
    }

    // this needs to match the entitlements and provisioning profile
    NSURL *cloudURL = nil;
    if([fileManager respondsToSelector:@selector(URLForUbiquityContainerIdentifier:)])
    {
      cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
      NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"chefsbook_v14b3"];
      cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];
      DLog(@"cloudURL : %@", cloudURL);
    }

    //  The API to turn on Core Data iCloud support here.
    NSDictionary *options = nil;
    if (cloudURL) {
      options = [NSDictionary dictionaryWithObjectsAndKeys:
                 @"com.thelostsouls.chefsbook", NSPersistentStoreUbiquitousContentNameKey,
                 cloudURL, NSPersistentStoreUbiquitousContentURLKey,
                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption
                 ,nil];
    } 

    NSError *error = nil;
    [persistentStoreCoordinator_ lock];
    DLog(@"Persistent Store ****LOCKED****");

    /**
     If I migrate url is set don't just add, migrate!
     Otherwise proceed as planned.
     */
    if (migrateUrl) {
      NSPersistentStore *srcPS = [persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType
                                                configuration:nil
                                                          URL:migrateUrl
                                                      options:nil
                                                        error:&error];
      if (![persistentStoreCoordinator_ migratePersistentStore:srcPS
                                                         toURL:storeUrl
                                                       options:options
                                                      withType:NSSQLiteStoreType
                                                         error:&error]) {
        DLog(@"Error migrating data: %@, %@", error, [error userInfo]);
        abort();
      }
    }
    else
    {
      if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType
                                                     configuration:nil
                                                               URL:storeUrl
                                                           options:options
                                                             error:&error]) {
        DLog(@"Error adding persistent store: %@, %@", error, [error userInfo]);
        abort();
      }
    }
    [persistentStoreCoordinator_ unlock];
    DLog(@"Persistent Store ****UNLOCKED****");

    // tell the UI on the main thread we finally added the store and then
    // post a custom notification to make your views do whatever they need to such as tell their
    // NSFetchedResultsController to -performFetch again now there is a real store
    dispatch_async(dispatch_get_main_queue(), ^{
      DLog(@"asynchronously added persistent store!");
      [[NSNotificationCenter defaultCenter]
       postNotificationName:AppDelegateSharedPSCAddedStore
       object:self userInfo:nil];
    });
  });

}

So there you go.

Still a worry are scenarios where iCloud support is enabled/disabled and/or the data deleted from the cloud via the settings menu. Hopefully nobody would do that and leave iCloud enabled or they’d end up with an empty application and no recovery. That’s also the reason I’m still supporting and improving the code that saves the docs out to iTunes.

Another thought just occurred to me. The following scenario needs serious consideration:

  1. User starts app with iCloud support, initial data set loaded.
  2. User proceeds to add a handful of data.
  3. User installs Chef’s Book on second device with iCloud enabled for app.
  4. App starts and copies in default data PLUS receiving iCloud data.

Will it fail, magically know it’s the same (eminently probable), or duplicate data?

Time to find out!

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Well I found out, it hangs permanently. Thanks to some helpful people on the apple forums I know why. So the above code is only partially correct. What needs to be done is to also query data on the cloud using NSMetadataQuery and examine the results, if there is cloud data you should use that and not add a persistent store in the normal way (I guess?). What I now need to find out, having successfully been able to query the cloud and find LOTS of data on there from my previous tests, is how you should add that data.

Then I’m going to rewrite the entire core data start up code from the bottom up.

Strictly speaking this isn’t a post about core data, honest. It’s ACTUALLY a post about how iPhone applications are deployed and updated. My shipped application contained two data models, my new application also contains two data models. One of the new versions data models is an old one renamed and another is entirely new. For some ridiculous reason I thought if I deleted something from my applications bundle it would be similarly deleted on the iPhone. That’s not the case. Thus the application ends up with 4 data models. This causes problems as the code I previously posted, cribbed from elsewhere in part, will happily migrate backwards if it finds a suitable model. I’d not thought of this as a strict problem if I was in control of what models were present, it seems I am not though.

UPDATE: After seeing some discussions about this issue and people saying it’s how XCode pushes out the updates I tested a bit more. XCode only pushes out files that have changed to ensure a quick testing cycle, it does not currently delete files. To confirm this I built an ad-hoc ipa file and installed that on my phone with iTunes. The files are indeed deleted. Therefore this is potentially a bug with XCode, it should delete files that are no longer in the bundle even if it isn’t pushing out a full update like iTunes does. It can take shortcuts, sure it has to I mean imagine testing something like Inifinity Blade any other way?, but should really ape the real world as much as possible and therefore should delete files.
End of

Of course you shouldn’t delete data models from your application after it’s deployed as you cannot dictate that somebody will upgrade to each release of your app. If the data model really was deleted from your bundle the user would be stuck as the software could not open the database to migrate.

There’s an argument to be made there, I am sure, for not using Core Data on simple or incredibly dynamic projects but that could be one for another day.

I do like the code I have got though and will not be abandoning it as it’s lightweight in terms of memory. Much more so than the other options. The use of NSOperations pushed into a queue as the migrations procede gives it remarkable power and flexibility too. There is plenty that can be done inside the migration loop to ensure it proceeds forwards and not backwards.

It seems that when you create a mapping model for core data migrations using XCode (3 or 4) it will not create mapping models for abstract entities. If you do not create this manually within your mapping model any data contained therein will be lost. In my case I needed to tie the image data back to the entity it belonged to. I could create several entities each with the requisite attributes but it made more sense to create one abstract image entity and have the others inherit from it. This enabled me to have a recipeimage and stepimage that could have relationships back to their parent without having to create lots of attributes. RecipeImage has Image as a parent and has a defined relationship with Recipe etc. The downside is that after migration if you do not create the Image mapping manually all you images vanish from the database.
I cannot fathom a good reason for this mapping not being created automatically, under what circumstances would you not want the data contained in an abstract entity migrated when you update the schema? At least it was easy to fix.

It’s not brilliantly efficient but it does reuse existing importing code and it does work reliably and well.  If I update the app to include new recipes I only want to include them once.  When the app is first launched a preloaded Core Data sqlite file is copied into the Documents directory.  To provide new recipes to existing users I could have included them as a set of chefdoc files to import from the applications bundle but that would be wasteful as they’d already be in the preloaded file.  So, expanding a bit on the previous use of automatic migrations we can use an NSEntityMigrationPolicy just like before, it happens that I have added more information to the DB so I needed one anyway.  Alongside a routine much like the one used previously we can add the following routine:

- (BOOL)endEntityMapping:(NSEntityMapping *)mapping
                 manager:(NSMigrationManager *)manager
                   error:(NSError **)error {
    if (![super endEntityMapping:mapping manager:manager error:error]) return NO;

    [self addNewRecipes:manager];

    return YES;
}

Inside the addNewRecipes routine we first grab our own NSPersistentStoreCoordinator and NSManagedObjectContexts:

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSError *error = nil;
    NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"Chefs_Book" ofType:@"momd"];
    NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
    NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc]
                                                initWithContentsOfURL:modelURL];    

    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
    NSString *initialDataPath = [[NSBundle mainBundle]
                                 pathForResource:@"InitialData" ofType:@"sqlite"];
    NSURL *initialStoreURL = [NSURL fileURLWithPath:initialDataPath];
    [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                             configuration:nil
                                                       URL:initialStoreURL
                                                   options:nil
                                                     error:&error];

    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    [context setPersistentStoreCoordinator:persistentStoreCoordinator];

Don’t forget to drain the pool at the end of the above! We can then set about iterating through the Recipes. I simply build a list of unique ID’s from our preloaded db, compare it to the users database using the passed in NSMigrationManager to reference the destination context like so:

    NSArray *destRecipeUUIDs = [KookaDIS allRecipeUUID:[manager destinationContext]];

Once I’ve got a list of which ones are missing I convert them into the same format that’s used for emailing then use the same routines to import the data that are used when somebody opens a file in the email application or in safari. It’s not the most efficient as images are converted and base64 encoded and decoded etc. but it has two major advantages:

- The routines are proven and reliable.
- I only have one set of bulk import/export routines to maintain should my needs change in the future.

Having said that, the routines involved are recursive, data independent and obey a set of rules passed in to avoid looping recursion so not many data base schema changes will make it throw a fit.

That is the last big bit of code done for the update. To do now is the progress bar code for data updating/import, some polish (simple UI changes like hiding icons instead of deactivating them to fall more inline with apples UI recommendations) then test it for memory likes and finally do end to end tests on hardware. Probably another week so perhaps two weeks until the update is in the store.

As the project nears release my thoughts turn to what will be needed in the near future and how best to solve an intermittent bug that’s present now. The bug was resolving by doing something I should have done ages ago, be consistent with how I use data in table views. When developing the app the most complicated view did not use NSFetchedResultsController as it required 2 of them, one to drive each section.
Implementing this bought another problem to the fore, how to identify recipes uniquely in the DB? The obvious method is to use the objectID that core data uses but this changes until the recipe is finalised by saving it. The problem there is driving the sections from NSFetchResults whilst the recipe is being created. Each addition can change the objectID invalidating a simple predicate such as:

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"recipe== %@",[recipe objectID]]];

The simplest solution is to create the recipe and immediately save it, blank, to the database then present it to the view. I have issues with that, crashes could lead to blank recipes polluting the integrity of the database and all sorts of problems. My minimum requirement is for the recipe to have a title (non unique) at least. So the solution I’ve known was needed has been implemented, UUID’s. For ease of use you want something that:

  • Adds UUIDs as part of the core db migration when the new managed object model is loaded.
  • Automatically creates and adds a UUID to a newly inserted recipe.

To help with this I created a helper class with the function:

+ (NSString*)UUIDString {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [NSMakeCollectable(string) autorelease];
}

To ensure the UUID is added to the core data object every time a recipe is created you can add this to the NSManagedObject subclass:

- (void)awakeFromInsert {
	[super awakeFromInsert];
	[self setValue:[KookaDIS UUIDString] forKey:@"uuid"];
}

Now for the method to update the database upon launch. Assuming you have automatic migrations turned on with the NSMigratePersistentStoresAutomaticallyOption option you need to create a NSEntityMigration subclass. I didn’t choose the best name for mine but it looks like this:

#import 

@interface AddUUIDToRecipeEMP : NSEntityMigrationPolicy {

}

@end
#import "AddUUIDToRecipeEMP.h"
#import "KookaDIS.h"

@implementation AddUUIDToRecipeEMP

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject*)src
									  entityMapping:(NSEntityMapping*)map
											manager:(NSMigrationManager*)mgr
											  error:(NSError**)error
{
	NSManagedObjectContext *destMOC = [mgr destinationContext];
	NSString *destEntityName = [map destinationEntityName];

	NSManagedObject *dest = [NSEntityDescription insertNewObjectForEntityForName:destEntityName
														  inManagedObjectContext:destMOC];

	NSArray *keys = [[[src entity] attributesByName] allKeys];
	NSDictionary *values = [src dictionaryWithValuesForKeys:keys];

	[dest setValuesForKeysWithDictionary:values];
	[dest setValue:[KookaDIS UUIDString] forKey:@"uuid"];
	// 511 is binary 111 111 111 or chmod 777.
	[dest setValue:[NSNumber numberWithInt:511] forKey:@"permissions"];

	[mgr associateSourceInstance:src withDestinationInstance:dest forEntityMapping:map];

	return YES;
}

@end

Ignore the permissions setValue for now, that’s for when we’ve covered the new UUID updates. So we now have a custom entity migration class. To use it create a new file in your project choosing “iOS->Resource->Mapping Model”. Within the mapping model click on the pre-made entity mapping that is relevant for you. In the far right hand pain you should see a Custom Policy field, simply type the name of the class you made in there. That’s it. Afaict this turns off any other automatic migration for that record so if you have other alterations you will need to code those changes manually too.

The above changes mean that the predicate for getting the data for the sections is consistent whether the recipe is new or not. It becomes:

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"recipe.uuid == %@",recipe.uuid]];

The other thing I have considered is the potential inclusion of other recipes with permission and permissions for user uploads to the server. So I implemented a quick bitwise permissions system based on linux simple user/group/world permissions for read/write/execute. I’ve modified it to creater/owner/world. Owner would be someone with explicit permission or (heaven forbid) in app purchase and world would be any other download/import.

I felt that storing the data as an int is the simplest solution but to present that to the rest of the program in such a way would be anachronistic in such a modern language so I needed two routines to convert the info from an int to an NSDictionary and vice versa. Thus these two routines were born and added to the afore mentioned class:

NSString * const kKDISShareEnabled = @"KookaDocumentShareable";
NSString * const kKDISEditEnabled = @"KookaDocumentEditable";
NSString * const kKDISPrintEnabled = @"KookaDocumentPrintable";

NSString * const kKDISCreator = @"KookaDocumentCreator";
NSString * const kKDISOwner = @"KookaDocumentOwner";
NSString * const kKDISWorld = @"KookaDocumentWorld";

+ (NSDictionary *)permissionsDictionaryFromNumber:(NSNumber *)permissions {
	NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
	int value = [permissions intValue];
	int creator = value & 0x7;
	int owner = value >> 3 & 0x7;
	int world = value >> 6 & 0x7;

	NSNumber *cs = [NSNumber numberWithInt:creator & 0x1];
	NSNumber *ce = [NSNumber numberWithInt:creator >> 1 & 0x1];
	NSNumber *cp = [NSNumber numberWithInt:creator >> 2 & 0x1];
	NSNumber *os = [NSNumber numberWithInt:owner & 0x1 ];
	NSNumber *oe = [NSNumber numberWithInt:owner >> 1 & 0x1];
	NSNumber *op = [NSNumber numberWithInt:owner >> 2 & 0x1];
	NSNumber *ws = [NSNumber numberWithInt:world & 0x1];
	NSNumber *we = [NSNumber numberWithInt:world >> 1 & 0x1];
	NSNumber *wp = [NSNumber numberWithInt:world >> 2 & 0x1];

	[dictionary setObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:cs,ce,cp,nil]
													  forKeys:[NSArray arrayWithObjects:kKDISShareEnabled,kKDISEditEnabled,kKDISPrintEnabled,nil]]
				   forKey:kKDISCreator];
	[dictionary setObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:os,oe,op,nil]
													  forKeys:[NSArray arrayWithObjects:kKDISShareEnabled,kKDISEditEnabled,kKDISPrintEnabled,nil]]
				   forKey:kKDISOwner];
	[dictionary setObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:ws,we,wp,nil]
													  forKeys:[NSArray arrayWithObjects:kKDISShareEnabled,kKDISEditEnabled,kKDISPrintEnabled,nil]]
				   forKey:kKDISWorld];

	return dictionary;
}

+ (NSNumber *)permissionsNumberFromDictionary:(NSDictionary *)dictionary {
	int value = 0;
	int world = [[[dictionary valueForKey:kKDISWorld] valueForKey:kKDISPrintEnabled] intValue] << 2;
	world |= [[[dictionary valueForKey:kKDISWorld] valueForKey:kKDISEditEnabled] intValue] << 1;
	world |= [[[dictionary valueForKey:kKDISWorld] valueForKey:kKDISShareEnabled] intValue];
	int owner = [[[dictionary valueForKey:kKDISOwner] valueForKey:kKDISPrintEnabled] intValue] << 2;
	owner |= [[[dictionary valueForKey:kKDISOwner] valueForKey:kKDISEditEnabled] intValue] << 1;
	owner |= [[[dictionary valueForKey:kKDISOwner] valueForKey:kKDISShareEnabled] intValue];
	int creator = [[[dictionary valueForKey:kKDISCreator] valueForKey:kKDISPrintEnabled] intValue] << 2;
	creator |= [[[dictionary valueForKey:kKDISCreator] valueForKey:kKDISEditEnabled] intValue] << 1;
	creator |= [[[dictionary valueForKey:kKDISCreator] valueForKey:kKDISShareEnabled] intValue];

	value |= world << 6 & 0x1c0;
	value |= owner << 3 & 0x38;
	value |= creator & 0x7;

	return [NSNumber numberWithInt:value];
}

I am not used to coding bitwise operations so my use of odd masks may be a very poor way of doing it but it works.

Why does the following code throw an exception and immediately sig kill on the second iteration at [e nextObject]:

		for (NSManagedObject *current in categories) {
			if ([lowercaseName isEqualToString:[current.name lowercaseString]] && category != current ) {

				// Move all the recipes.
				NSEnumerator *e = [[category valueForKey:@"recipe"] objectEnumerator];
				id object;
				while (object = [e nextObject]) {
					[(NSManagedObject *)object setCategory:current];
				}
				// Delete the category we imported.
				[moc deleteObject:category];

				// Reassign the current category to be returned instead.
				category = current;
				break;
			}
		}

Please note that in the above code, implied but not shown, category is an NSManagedObject and categories is an NSSet of NSManagedObjects. Also category will ALWAYS be present in categories.

The code above is intended to stop duplication of an NSManagedObject by finding out if one already exists and if it does moving it’s objects over to it.

The reason it crashes is because these objects (being NSManagedObjects) are very closely tied to the underlying database. After the first iteration through the while loop we have changed the underlying database, this change is immediately reflected in our NSManagedObject and therefore immediately reflected in the iterator. For example:

1) while (object = [e nextObject]) {

The iterator ‘e’ is attached to the managed object ‘category’, it contains 2 objects. The first of which is assigned to ‘object’.

2) [(NSManagedObject *)object setCategory:current];

The managed object ‘object’ is re-assigned away from the managed object ‘category’ to the managed object ‘current’. The managed object ‘category’ therefore now only has one object.

3) while (object = [e nextObject]) {

An exception is thrown. The NSEnumerator (actually an NSFastEnumerator something or other) can also be written in code as:

for (object in category)

Internally it behaves very very much like a for loop using pointer arithmetic. For this reason on the second loop the pointer is no longer valid as what was object no. 2 is now object no. 1. We have moved the object to another NSManagedObject and the change is reflected immediately. The contents of the while loop can be modified to anything that does not move the objects within ‘category’ and the crash goes away.

What do we do about this? Well the first thought is to work on a copy of the data. Thankfully we are given very simple tools to make copies such as:

NSSet *fishes = [[atlantic fish] copy];
NSMutableSet *fishes = [[pacific fish] mutableCopy];

Right? In this instance yes. Be aware however that this will not always solve your problems. It might give you a copy of the surface data but it does NOT give you a deep copy of the underlying objects. So fishes is not the same as atlantic but it will contain the same ‘fish’. If you follow my rather horrible example. I expect it’s influenced by the tropical fish tank and conversation I was having about Danio’s whilst I write this. The fixed code reads:

		for (NSManagedObject *current in categories) {
			if ([lowercaseName isEqualToString:[current.name lowercaseString]] && category != current ) {

				// Move all the recipes.
				NSSet *workingCopy = [[category item] copy];
				NSEnumerator *e = [[category valueForKey:@"item"] objectEnumerator];
				id object;
				while (object = [e workingCopy]) {
					[(NSManagedObject *)object setCategory:current];
				}
				// Delete the category we imported.
				[workingCopy release];
				[moc deleteObject:category];

				// Reassign the current category to be returned instead.
				category = current;
				break;
			}
		}

Converting the JSON messages back into managed objects should be quite simple.  You loop around your dictionary converting stuff back.  A useful shortcut is provided by the managed object function setValuesForKeysWithDictionary.  Given that the dictionary came from a managed object in the first place we know that all of our keys should be in our object so:

2010-10-28 12:07:26.404 Cooking Companion[29814:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary managedObjectContext]: unrecognized selector sent to instance 0x8250370'

Or not. So we assume that the instance in question is inside my NSDictionary but we can’t tell which one as po will print the contents but not the addresses of the objects. We could override the NSDictionary description function to print them out but we only need to do this for debugging and there aren’t that many:

(gdb) print (id)[structureDictionary objectForKey:@"author"]
$1 = (id) 0x2a8dd68
(gdb) print (id)[structureDictionary objectForKey:@"category"]
$2 = (id) 0x841bf00
(gdb) print (id)[structureDictionary objectForKey:@"cookingtemp"]
$3 = (id) 0x8412f10
(gdb) print (id)[structureDictionary objectForKey:@"cookingtempunits"]
$4 = (id) 0x2a8dd68
(gdb) print (id)[structureDictionary objectForKey:@"cookingtime"]
$5 = (id) 0x841fe50
(gdb) print (id)[structureDictionary objectForKey:@"desc"]
$6 = (id) 0x841cbe0
(gdb) print (id)[structureDictionary objectForKey:@"gead"]
$7 = (id) 0x0
(gdb) print (id)[structureDictionary objectForKey:@"head"]
$8 = (id) 0x0
(gdb) print (id)[structureDictionary objectForKey:@"heat"]
$9 = (id) 0x841fde0
(gdb) print (id)[structureDictionary objectForKey:@"ingredients"]
$10 = (id) 0x841fa60
(gdb) print (id)[structureDictionary objectForKey:@"method"]
$11 = (id) 0x2a8dd68
(gdb) print (id)[structureDictionary objectForKey:@"photo"]
$12 = (id) 0x2a8dd68
(gdb) print (id)[structureDictionary objectForKey:@"preparationtime"]
$13 = (id) 0x841cb40
(gdb) print (id)[structureDictionary objectForKey:@"steps"]
$14 = (id) 0x841e750
(gdb) print (id)[structureDictionary objectForKey:@"style"]
$15 = (id) 0x2a8dd68
(gdb) print (id)[structureDictionary objectForKey:@"title"]
$16 = (id) 0x841f570
(gdb) print (id)[structureDictionary objectForKey:@"utensils"]
$17 = (id) 0x2a8dd68
(gdb) print structureDictionary
$18 = (NSMutableDictionary *) 0x841fd40
(gdb) print managedObject
$19 = (NSManagedObject *) 0x8421b50
(gdb) print moc
$20 = (NSManagedObjectContext *) 0x8502340
(gdb) n
2010-10-28 13:43:37.908 Cooking Companion[30053:207] -[__NSCFDictionary managedObjectContext]: unrecognized selector sent to instance 0x841bf00

Unsurprisingly it’s Category. Which is the first relationship it would have come to I suppose. Should’ve guessed it would be relationships causing trouble! :P So it looks like we should convert things depth first then assign.

To me it seems that documentation on populating apple Core Data based stores is sorely lacking instructions on how to populate the database at runtime from remote sources. Most people electing to populate an SQL Lite database and then just copy it in when the app is first loaded.  UGH.  I wanted to retrieve the initial database at runtime and then also use this remote store to update my locally held database at the users request; merging the data intelligently.  The problem was trying to do it as the app launches and trying to do it at various points in the App Delegate or view controller startup screwed with stuff and caused EXC_BAD_ACCESS.  Which implies something somewhere got de-allocated/allocated when it shouldn’t have.  Here is the solution, the remote store is a plist or PropertyList, which is an xml format used a lot by apple and apple software.

I’ll assume a lot of prior knowledge here unless people comment saying they want more information.

Start a new project using core data, in my case it’s a universal iPhone Navigation Bar project.  Within this project I deleted the default ‘Event’ database stuff it set up and created a database and relationships of my own.  In this example I only use one table called ‘Category’ which holds nothing but ‘name’.  A custom cell was created to display this information which also has a UIImageView field loading a local image based on the name of the category.  References in Root View Controller were changed from ‘Event’ to ‘Category’ and from ‘timestamp’ to ‘name’.   Ignoring the code to display the custom cell itself it necessitated edits to:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSFetchedResultsController *)fetchedResultsController

I’m not allowing editing here so insertNewObject is gone, but if you were you’d have to edit that to so you can move away from the example data.

The problem for me was not being able to ascertain where best to shove the following code to populate the database:


	// Read in property list
	NSPropertyListFormat *format;
	NSString *errorDesc;
	NSData *plistXML = [[NSData alloc] initWithContentsOfURL:source];

	NSDictionary *data = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML
																				   mutabilityOption:NSPropertyListImmutable format:format errorDescription:&errorDesc];

	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]];
		}
	}

	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_]];
	[categoriesRequest setPredicate:[NSPredicate predicateWithFormat:@"(name in %@)",categories]];

	// Sort our data store categories too like
	[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
	NSEnumerator *enumurateDSCategories = [categoriesMatchingNames objectEnumerator];

	// Get the names to parse
	for(NSString *type in categories) {
		Category *dsCategory = (Category *)[enumurateDSCategories nextObject];
		if( (dsCategory == nil) && ![type isEqualToString:[dsCategory name]] ) {
			Category *category = (Category *)[NSEntityDescription insertNewObjectForEntityForName:@"Category" inManagedObjectContext:managedObjectContext_];
			[category setName:type];
		}
	}

	if(![managedObjectContext_ save:&error])
	{
	 NSLog(@"Core data failed to save new objects!");
	}

	[categories release];
	[plistXML release];

	return TRUE;

I tried placing this in:


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

and viewDidLoad, viewDidAppear and others within the Root View Controller. None worked, they populated the database but then crashed. I finally decided that the best approach would be to argue that if I had no categories defined the other data isn’t there either. To begin with I settled on a modal dialogue but your approach could vary. In the App Delegate I defined two functions, one to do the actual work later on, not yet implemented except for Category, and the other to use a local xml file initially anyway:


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

in the Root View Controller source file (RootViewController.m) the following is modified/added:


- (void)viewDidAppear:(BOOL)animated {
	id  sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
	if ( [sectionInfo numberOfObjects] < 1) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Data."
														message:@"Initial data not found, loading defaults.\nHit cancel  and quit to configure a remote source in Settings.app."
													   delegate:self
											  cancelButtonTitle:@"Cancel"
											  otherButtonTitles:nil];
		[alert addButtonWithTitle:@"OK"];
		[alert show];
		[alert release];
	}

    [super viewDidAppear:animated];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
	if ( buttonIndex == 1 )
		[((Delicious_LibraryAppDelegate *)[[UIApplication sharedApplication] delegate]) importData];
}

The above counts how many rows I have in my main table (which has one column, hence objectAtIndex is 0) and if it is less than 1 I assume we have no data and prompt the user. When the user hits OK the importData function is called. For now it just loads the local data but it could check the app settings and use the remote function.

Powered by WordPress | Theme: KLG based on Motion by 85ideas.