Permalink
Browse files

Start support for immediate use of new JSON

  • Loading branch information...
BasThomas committed Jul 24, 2016
1 parent 89613a2 commit 4642d0806d25ac69d381959d8348c2dee891e73e
Showing with 22 additions and 9 deletions.
  1. +4 −1 trySwift/AppDelegate.swift
  2. +11 −5 trySwift/JSONManager.swift
  3. +7 −3 trySwift/Networking.swift
@@ -16,7 +16,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
configureStyling()
- Networking.refreshJSONData()
+ Networking.refreshJSONData { updated in
+ guard updated else { return }
+ // Use updated json file in app
+ }
NSTimeZone.setDefaultTimeZone(NSTimeZone(abbreviation: "JST")!)
return true
View
@@ -43,14 +43,17 @@ enum JSONManager {
}
}
- static func save(JSON json: JSON, with version: Double) {
- guard let fileName = jsonFile(for: version) else { return }
+ static func save(JSON json: JSON, with version: Double) -> Bool {
+ guard let fileName = jsonFile(for: version) else { return false }
do {
- try json.serialize().writeToFile(fileName, atomically: true)
+ let result = try json.serialize().writeToFile(fileName, atomically: true)
defaults.setDouble(version, forKey: "version")
+ return result
} catch {
print(error)
}
+
+ return false
}
enum DecodeError: ErrorType {
@@ -59,15 +62,18 @@ enum JSONManager {
case InvalidFileName
case NoData
}
-
+}
+
+private extension JSONManager {
+
/// Returns the first path in the document directory.
/// The return value is an NSString so it allows `stringByAppendingPathComponent(_:)`
/// to be called on it.
private static var documentsDirectory: NSString? {
return NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first
}
- static func jsonFile(for version: Double) -> String? {
+ private static func jsonFile(for version: Double) -> String? {
return documentsDirectory?.stringByAppendingPathComponent("data-\(version).json")
}
}
View
@@ -43,11 +43,15 @@ enum Networking {
}
}
- static func refreshJSONData() {
+ static func refreshJSONData(completionHandler: (updated: Bool) -> Void) {
networkJSONVersion { version in
- guard version != defaults.doubleForKey("version") else { return }
+ guard version != defaults.doubleForKey("version") else {
+ completionHandler(updated: false)
+ return
+ }
networkJSONData(forVersion: version) { json in
- JSONManager.save(JSON: json, with: version)
+ let result = JSONManager.save(JSON: json, with: version)
+ completionHandler(updated: result)
}
}
}

0 comments on commit 4642d08

Please sign in to comment.