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

   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