This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import CoreData | |
| @propertyWrapper | |
| class Fetched<Object: NSManagedObject>: NSObject, NSFetchedResultsControllerDelegate { | |
| private var _fetchedResultsController: NSFetchedResultsController<Object>? | |
| var wrappedValue: [Object] = [] | |
| init(context: NSManagedObjectContext = CoreDataService.context, sortDescriptiors: [NSSortDescriptor] = [], predicate: NSPredicate? = nil) { | |
| super.init() | |
| // Setup a fetch request |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import CoreData | |
| @propertyWrapper | |
| class Fetched<Object: NSManagedObject>: NSObject, NSFetchedResultsControllerDelegate { | |
| private var _fetchedResultsController: NSFetchedResultsController<Object>? | |
| var wrappedValue: [Object] = [] | |
| init(context: NSManagedObjectContext = CoreDataService.context, sortDescriptiors: [NSSortDescriptor] = [], predicate: NSPredicate? = nil) { | |
| super.init() | |
| // Setup a fetch request |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| TodoService().getTodo(todo: Todo.placeholder(id: 1)) | |
| .sink(receiveCompletion: { | |
| (completion) in | |
| print(completion) | |
| }) { (todo) in | |
| print(todo) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Router { | |
| static var baseUrl = APIConfig.baseURL | |
| @available(iOS 13.0, *) | |
| public static func performRequest<R: Route>(_ route: R) -> AnyPublisher<R.ResponseType, Error> { | |
| return URLSession.shared.dataTaskPublisher(for: route.toRequest()) | |
| .mapError { NetworkError(from: $0) } | |
| .map(\.data) | |
| .decode(type: R.ResponseType.self, decoder: JSONDecoder()) | |
| .mapError { _ in NetworkError.decodingFailed } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import Combine | |
| public class TodoService { | |
| public func getTodo(todo: Todo) -> AnyPublisher<Todo, Error> { | |
| return Router.performRequest(GetTodoRoute(todo: todo)) | |
| .receive(on: DispatchQueue.main) | |
| .eraseToAnyPublisher() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// A route that gets a single `Todo`. | |
| public struct GetTodoRoute: Route { | |
| public typealias RequestType = Empty | |
| public typealias ResponseType = Todo | |
| public static let method = HTTPMethod.get | |
| public static let path = "todos" | |
| public static let requiresAuth = false | |
| public var requestObject: Empty? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Route { | |
| /// Default parameters so routes do not need to declare these | |
| var queryParameters: [URLQueryItem] { | |
| return [] | |
| } | |
| var pathParameters: [String] { | |
| return [] | |
| } | |
| /// The basic implementation of `Route` `toRequest` adds parameters and builds the path. | |
| public func toRequest() -> URLRequest { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public protocol Route { | |
| /// Request & ResponseType are forced, but `Empty` allows you to skip encoding/decoding | |
| associatedtype RequestType: Encodable | |
| associatedtype ResponseType: Decodable | |
| static var method: HTTPMethod { get } | |
| static var path: String { get } | |
| static var requiresAuth: Bool { get } | |
| /// Optional instance of `RequestType` that gets encoded if present | |
| var requestObject: RequestType? { get set } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public protocol Route { | |
| /// Request & ResponseType are forced, but `EmptyRequest` allows you to skip encoding/decoding | |
| associatedtype RequestType: Encodable | |
| associatedtype ResponseType: Decodable | |
| static var method: HTTPMethod { get } | |
| static var path: String { get } | |
| /// Optional instance of `RequestType` that gets encoded if present | |
| var requestObject: RequestType? { get set } | |
NewerOlder