Skip to content

Instantly share code, notes, and snippets.

@pyrabbit
Last active June 6, 2016 22:53
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 pyrabbit/6b710c6bba1f9ed6e6421a0e4cb44db3 to your computer and use it in GitHub Desktop.
Save pyrabbit/6b710c6bba1f9ed6e6421a0e4cb44db3 to your computer and use it in GitHub Desktop.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
updateStylesFromServer()
return true
}
// MARK: - Remote Style Updating
func updateStylesFromServer() {
Alamofire.request(.GET, "https://consultant-aide.firebaseio.com/brands/lularoe.json")
.responseJSON { response in
// setting app delegate to prepare to save to CoreData
//let app = UIApplication.sharedApplication().delegate as! AppDelegate
//let context = app.managedObjectContext
let entity = NSEntityDescription.entityForName("Style", inManagedObjectContext: self.managedObjectContext)
if let data = response.result.value {
self.removeStylesFromDevice()
for (_,object) in JSON(data) {
let style = Style(entity: entity!, insertIntoManagedObjectContext: self.managedObjectContext)
style.brand = "lularoe"
style.name = object["name"].stringValue
style.sizes = object["sizes"].arrayValue.map { $0.string! }
print("Adding: " + style.name!)
self.managedObjectContext.insertObject(style)
}
}
}
}
func removeStylesFromDevice() {
let fetchRequest = NSFetchRequest(entityName: "Style")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
print("Removing existing styles from device")
try persistentStoreCoordinator.executeRequest(deleteRequest, withContext: self.managedObjectContext)
} catch let err as NSError {
print("Could not delete styles from device")
print(err.debugDescription)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment