Skip to content

Latest commit

 

History

History
 
 

TaggedLocations

TaggedLocations

Apple's 'TaggedLocations' CoreData sample application, modified to use FatFractal as its cloud backend.

Run up a FatFractal engine on your dev machine

( see http://fatfractal.com/docs/getting-started/ )

Deploy this sample locally

cd /path/to/fatfractal-code-samples/TaggedLocations/FatFractal
ffef deploylocal

Open and run TaggedLocations.xcodeproj

The app is simple - it allows the user to create "events" and tag them, and stores all data on the device using CoreData. The original ReadMe is here

I've made some simple modifications to

  • Also store all of the user's events, along with all of the tagsm in a FatFractal backend
  • 'sync' the user's events when the app starts up, by retrieving any events that have been modified, for example on another device

Overview of the mods to the original Apple sample code:

  • Let the FatFractal SDK know how to handle your CoreData objects, by creating a custom FatFractal subclass and modifying the AppDelegate to create an instance of it

    • This is the code that holds everything together. We're over-riding
    - (id) createInstanceOfClass:(Class) class forObjectWithMetaData:(FFMetaData *)objMetaData
    • so that when the FatFractal SDK needs to create an instance of one of your objects, then you can control how that's done.
    • In this example, then if it's an NSManagedObject subclass, we're first checking to see if we already have that object locally, and if not then we're calling the appropriate CoreData initializer.
        - (id) createInstanceOfClass:(Class) class forObjectWithMetaData:(FFMetaData *)objMetaData {
            if ([class isSubclassOfClass:[NSManagedObject class]]) {
                id obj = [self findExistingObjectWithClass:class andFFUrl:objMetaData.ffUrl];
                if (obj) {
                    NSLog(@"Found existing %@ object with ffUrl %@ in managed context", NSStringFromClass(class), objMetaData.ffUrl);
                    return obj;
                } else {
                    NSLog(@"Inserting new %@ object with ffUrl %@ into managed context", NSStringFromClass(class), objMetaData.ffUrl);
                    return [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass(class)
                                                         inManagedObjectContext:self.managedObjectContext];
                }
            } else {
                return [[class alloc] init]; // You MUST provide this as a default
            }
        }
  • Added an 'ffUrl' property to APLEvent and APLTag

    • While not the only way, this is the simplest way possible to handle both the 'unique id' issue as well as allowing FatFractal's object REFERENCEs to work seamlessly
  • Changed the name of the 'creationDate' property in APLEvent (and the core data model) to 'createdAt' (which is one of FatFractal's default built-in metadata attributes)

  • Modified APLEventsTableViewController so it first fetches from the CoreData store on the device, and then from the FatFractal backend

    • I've also added a 'lastRefreshDate' property, stored locally in NSUserDefaults, to limit the data that is pulled from the backend
  • Added code to create or update an event on the backend as required

  • Added code to delete an event from the FatFractal backend (uses 'queueing' for this so if your app is offline, the delete will be processed when it reconnects)

  • Adding basic 'login' functionality

  • Similarly, APLTagSelectionController will fetch tags from the backend, create, update and delete tags, and add or remove a tag from an event.

    • Note also the "- (BOOL)shouldSerialize" method on both APLEvent and APLTag - in this example, a many-to-many example, we don't wish to serialize that relationship - instead we are using FatFractal's "grab-bag" methods.