Skip to content

Instantly share code, notes, and snippets.

@foffer
Created June 16, 2014 12:36
Show Gist options
  • Save foffer/04f92649d2d491eb75f0 to your computer and use it in GitHub Desktop.
Save foffer/04f92649d2d491eb75f0 to your computer and use it in GitHub Desktop.
func loadDesserts() {
println("loadDesserts beginning")
var error: NSError?
var objectManager: RKObjectManager = RKObjectManager(baseURL: NSURL(string: "http://192.168.0.3:4000"))
let modelURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Kogekunst", ofType: "momd"))
println(objectManager)
AFNetworkActivityIndicatorManager.sharedManager().enabled = true
var managedObjectModel: NSManagedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL).mutableCopy() as NSManagedObjectModel
let managedObjectStore: RKManagedObjectStore = RKManagedObjectStore(managedObjectModel: managedObjectModel)
let storePath = RKApplicationDataDirectory().stringByAppendingPathComponent("dataStore.sqlite")
var persistentStore = managedObjectStore.addSQLitePersistentStoreAtPath(storePath,
fromSeedDatabaseAtPath: nil,
withConfiguration: nil,
options: [NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption : true],
error: &error)
objectManager.managedObjectStore = managedObjectStore
objectManager.managedObjectStore.createManagedObjectContexts()
let dessertMapping = RKEntityMapping(forEntityForName: "Dessert", inManagedObjectStore: managedObjectStore)
dessertMapping.addAttributeMappingsFromDictionary(["id" : "dessertID",
"name" : "name",
"header" : "header",
"howto": "mainText",
"image" : "image",
"created_at" : "createdAt"])
dessertMapping.identificationAttributes = ["dessertID"]
let ingredientMapping = RKEntityMapping(forEntityForName: "Ingredient", inManagedObjectStore: managedObjectStore)
ingredientMapping.addAttributeMappingsFromDictionary(["ingredient_amount" : "value",
"ingredient_unit" : "unit",
"ingredint_name" : "name",
"ingredint_id" : "ingredientID",
"ingredient_full" : "full"])
ingredientMapping.identificationAttributes = ["ingredientID"]
dessertMapping.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "ingredients",
toKeyPath: "ingredients",
withMapping: ingredientMapping))
let responseDescriptor = RKResponseDescriptor(mapping: dessertMapping,
method: RKRequestMethod.GET,
pathPattern: "/dishes.json",
keyPath: "dishes",
statusCodes: RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))
let context: NSManagedObjectContext = RKObjectManager.sharedManager().managedObjectStore.mainQueueManagedObjectContext
let request = NSURLRequest(URL: NSURL(string: "http://192.168.0.3:4000/dishes.json"))
let managedObjectRequestOperation = RKManagedObjectRequestOperation(request: request, responseDescriptors: [responseDescriptor])
managedObjectRequestOperation.managedObjectContext = context
managedObjectRequestOperation.managedObjectCache = managedObjectStore.managedObjectCache
NSOperationQueue.currentQueue().addOperation(managedObjectRequestOperation)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment