Skip to content

Instantly share code, notes, and snippets.

@nazmulkp
Created November 27, 2016 21:25
Show Gist options
  • Save nazmulkp/7eff3dc3b9000066a9ed70d9c3dd9e8e to your computer and use it in GitHub Desktop.
Save nazmulkp/7eff3dc3b9000066a9ed70d9c3dd9e8e to your computer and use it in GitHub Desktop.
Building the Signals
func fetchJSON(from url: URL) -> RACSignal {
print("Fetching: \(url.absoluteString)")
// 1
return RACSignal.createSignal({(_ subscriber: RACSubscriber) -> RACDisposable in
// 2
var dataTask = self.session.dataTask(withURL: url, completionHandler: {(_ data: Data, _ response: URLResponse, _ .error: Error) -> Void in
// TODO: Handle retrieved data
})
// 3
dataTask.resume()
// 4
return RACDisposable(block: {() -> Void in
dataTask.cancel()
})
}).doError({(_ error: Error) -> Void in
// 5
print("\(.error)")
})
}
@nazmulkp
Copy link
Author

nazmulkp commented Nov 27, 2016

    func fetchDailyForecast(forLocation coordinate: CLLocationCoordinate2D) -> RACSignal {
        var urlString = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=\(coordinate.latitude)&lon=\(coordinate.longitude)&units=imperial&cnt=7"
        var url = URL(string: urlString)!
        // Use the generic fetch method and map results to convert into an array of Mantle objects
        return self.fetchJSON(from: url).map({(_ json: [AnyHashable: Any]) -> Void in
                // Build a sequence from the list of raw JSON
            var list = .json["list"].rac_sequence()
            // Use a function to map results from JSON to Mantle objects
            return list.map({(_ item: [AnyHashable: Any]) -> Void in
                do {
                    return try MTLJSONAdapter.modelOfClass(WXDailyForecast.self, fromJSONDictionary: item)
                }
                catch {
                }
            })()
        })
    }
  }

@nazmulkp
Copy link
Author

nazmulkp commented Nov 27, 2016

   class func sharedManager() -> Self {
             var sharedManager: Any? = nil
             var onceToken: dispatch_once_t
            dispatch_once(onceToken, {() -> Void in
                  self.sharedManager = self.init()
           })

       return sharedManager
 }

@nazmulkp
Copy link
Author

   override init() {
        super.init()

        // 1
        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        // 2
        self.client = WXClient()
        // 3
        RACObserve(self, currentLocation).ignore(nil).flattenMap({(_ newLocation: CLLocation) -> Void in
            return RACSignal.merge([self.updateCurrentConditions(), self.updateDailyForecast(), self.updateHourlyForecast()])
            // 6
        }).deliver(on: RACScheduler.mainThreadScheduler).subscribeError({(_ error: Error) -> Void in
            TSMessage.showNotification(withTitle: "Error", subtitle: "There was a problem fetching the latest weather.", type: TSMessageNotificationTypeError)
        })
    
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment