Skip to content

Instantly share code, notes, and snippets.

@tomj
tomj / combineLatest.swift
Last active June 17, 2016 02:47
combineLatest on 2 network requests
import Result
import ReactiveCocoa
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let postsProducer = SignalProducer<AnyObject, NSError> { observer, disposable in
let url = NSURL(string:"http://jsonplaceholder.typicode.com/posts")!.URLByAppendingPathComponent("42")
let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) in
@tomj
tomj / rac_chain_multiple_requests.swift
Last active June 4, 2016 09:09
Chain multiple network requests using Reactive Cocoa 4 - for www.reactivebyexample.com
func getUserPosts(userID:String) -> SignalProducer<AnyObject, NSError> {
let postsProducer = SignalProducer<AnyObject, NSError> { observer, disposable in
let url = NSURL(string:"http://jsonplaceholder.typicode.com/posts")!.URLByAppendingPathComponent(userID)
let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) in
if error != nil {
observer.sendFailed(NSError(domain:"", code:5, userInfo:nil))
} else {
let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
observer.sendNext(json)
observer.sendCompleted()
@tomj
tomj / rac_serial_requests.swift
Last active May 22, 2016 07:08
Make 2 serial network requests with ReactiveCocoa in an Xcode Playground (with changes from @sebastiangrail)
import Result
import ReactiveCocoa
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let producer1 = SignalProducer<AnyObject, NSError> { observer, disposable in
let task1 = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string:"http://jsonplaceholder.typicode.com/posts/1")!, completionHandler: { (data, response, error) in
if error != nil {
@tomj
tomj / ff-github-swift-foundation.sh
Last active January 5, 2016 01:16
How to do a fast forward merge on GitHub for the Apple Swift Foundation project PR #213
$ git remote add hemet https://github.com/HeMet/swift-corelibs-foundation.git # add the contributor's repo as a remote
$ git remote update # grab the contributor's repo refs
$ git rebase origin/master master # update your local master branch to be up to date with origin
$ git checkout -b PR213 hemet/nsurl-fixes # make a local branch for the PR
$ git rebase master # rebase the PR branch on the most up to date master
$ git checkout master
$ git merge PR213 # fast-forward merge the PR branch on master
$ git push origin master # update master on the main repo
$ git branch -D PR213 # delete the PR branch from your local
$ git remote remove hemet # delete the contributor's repo remote