Last active
June 6, 2016 22:53
-
-
Save pyrabbit/6b710c6bba1f9ed6e6421a0e4cb44db3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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