Skip to content

Instantly share code, notes, and snippets.

View jemai's full-sized avatar

Abdelhak Jemaii jemai

  • Hamburg
View GitHub Profile
import XCTest
@testable import RealmDB_Manager
@testable import RealmSwift
@testable import Realm
class RealmDB_ManagerTests: XCTestCase {
struct service: LocalFileReader { }
struct db: Persister { }
var things = [Thing]()
extension Persister {
// MARK: - Read Methods
static func select<T: CodableObject>(type: T.Type, filter: NSPredicate? = nil) -> Results<T> {
do {
let realm = try Realm()
if let predicate = filter {
return realm.objects(T.self).filter(predicate)
} else {
return realm.objects(T.self)
@jemai
jemai / Persister.swift
Created June 1, 2019 18:04
Persister is a protocol that holds all the queries logic
protocol Persister {
static func select<T: CodableObject>(type: T.Type, filter: NSPredicate?) -> Results<T>
static func insert(object: CodableObject, update: Bool?)
static func insert(objects: [CodableObject], update: Bool?)
static func delete(object: CodableObject)
static func delete<T: CodableObject>(type: T.Type, filter: NSPredicate?)
static func deleteAll<T: CodableObject>(type: T.Type)
static func resetDB()
}
public protocol Requester {
static func execute<T: Codable>(ofType: T.Type, request : Request,
completion: @escaping (_ result: HTTPResult<T?,HTTPCallErrorType<Error>?>) -> Void)
}
struct service: Requester {}
func doSomething() {
// we are going to call this test api
let request = HTTPRequest(url: "http://ip.jsontest.com/")
service.execute(ofType: ResponseMapper.self, request: request) { (result) in
switch result {
case .failure(let error):
print(error)
@jemai
jemai / Request.swift
Last active March 13, 2019 16:12
Request protocol
import Foundation
public typealias Headers = [String: String]
public typealias Params = [String: String]
public typealias BodyParams = [String: Any]
public protocol Request {
var urlString : String? { get }
var method : HTTPMethod { get }
var bodyParams : BodyParams? { get }
extension Requester {
public static func execute<T: Codable>(ofType: T.Type, request : Request,
completion: @escaping
(_ result: HTTPResult<T?,HTTPCallErrorType<Error>?>) -> Void) {
// first we create the URLRequest
if let httpRequest = createRequest(from: request) {
// we use the nativ URL session to exute it
URLSession.shared.dataTask(with: httpRequest) { (data, response, error) in
if let error = error {
@jemai
jemai / HTTPCallErrorType.swift
Last active March 13, 2019 15:17
Error type
public enum HTTPCallErrorType<E> {
case urlError
case dataError
case responseError(E)
case mappingError(E)
}
public enum HTTPResult<U,V> {
case success(U)
case failure(V)
}
public enum HTTPMethod: String {
case get, post, put, patch, delete
}