Skip to content

Instantly share code, notes, and snippets.

@lucasecf
lucasecf / StreamingExample.swift
Last active February 24, 2023 23:16
Example of how to use the streaming feature of the iOS MultipeerConnectivity framework
/*
For this Gist, we have two sides: sender and receiver. The same user can be a sender and a receiver, but I will separate this
two roles to be more clear.
This gist assumes thatyou already have a MCSession created, and the peers are connected,
*/
@lucasecf
lucasecf / ClientConfig.swift
Last active November 15, 2016 03:42
Alamofire serialization example
//
// ClientConfig.swift
// EventCast
//
// Created by Lucas Eduardo on 27/10/15.
// Copyright © 2015 RNCDev. All rights reserved.
//
import Foundation
import Alamofire
@lucasecf
lucasecf / Model.swift
Created November 13, 2015 11:33
Example of a model that implements the ResponseSerializable protocol
//
// Event.swift
// EventCast
//
// Created by Lucas Eduardo on 27/10/15.
// Copyright © 2015 RNCDev. All rights reserved.
//
import Foundation
import CoreData
@lucasecf
lucasecf / ClientExample.swift
Created November 13, 2015 11:40
Example of ClientConfig use with Alamofire
//
// EventClient.swift
// EventCast
//
// Created by Lucas Eduardo on 27/10/15.
// Copyright © 2015 RNCDev. All rights reserved.
//
import Alamofire
@lucasecf
lucasecf / NSManagedObject+ActiveRecord.swift
Created November 13, 2015 11:46
Example of extension and subclass of NSManagedObject to use in all models
//
// NSManagedObject+ActiveRecord.swift
// EventCast
//
// Created by Lucas Eduardo on 04/11/15.
// Copyright © 2015 RNCDev. All rights reserved.
//
import Foundation
import CoreData
protocol Dispatcher {
func execute(request: URLRequest, success: (Data) -> Void, failure: (Error) -> Void)
}
protocol Dispatcher {
 associatedtype Output
  associatedtype Request
  func execute(request: Request, success: (Output) -> Void, failure: (Error) -> Void)
}
struct HTTPDispatcher: Dispatcher {
 func execute(request: URLRequest, success: (Data) -> Void, failure: (Error) -> Void) {
  // executes HTTP requests using URLSession
  // …
  }
}
struct TestHTTPDispatcher: Dispatcher {
  func execute(request: URLRequest, success: (Data) -> Void, failure: (Error) -> Void) {
 // to be used for unit tests, returns mocked data
let task1 = GetMoviesTask(dispatcher: HTTPDispatcher())
let task2 = GetMoviesTask(dispatcher: TestHTTPDispatcher())
struct GetMoviesTask {
let dispatcher: Dispatcher
func load(success: ([Movie]) -> Void, failure: (Error) -> Void) {
let request = URLRequest(url: URL(string: “www.mydomain.com/movies")!)
dispatcher.execute(request: request, success: { data in
// (…)
}, failure: { error in
// (…)
})