Skip to content

Instantly share code, notes, and snippets.

Avatar

Abdelhak Jemaii jemai

  • Hamburg
View GitHub Profile
View RealmDB_ManagerTests.swift
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]()
View Persister+defaultBehaviour.swift
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 Jun 1, 2019
Persister is a protocol that holds all the queries logic
View Persister.swift
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()
}
View Implementation.swift
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)
View Requester+DefaultBehaviour.swift
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 {
View Requester.swift
public protocol Requester {
static func execute<T: Codable>(ofType: T.Type, request : Request,
completion: @escaping (_ result: HTTPResult<T?,HTTPCallErrorType<Error>?>) -> Void)
}
View HTTPResult.swift
public enum HTTPResult<U,V> {
case success(U)
case failure(V)
}
View HTTPMethod.swift
public enum HTTPMethod: String {
case get, post, put, patch, delete
}
View HTTPCallErrorType.swift
public enum HTTPCallErrorType<E> {
case urlError
case dataError
case responseError(E)
case mappingError(E)
}
@jemai
jemai / Request.swift
Last active Mar 13, 2019
Request protocol
View Request.swift
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 }