A base architecture written by swift and protocol oriented. Created by Klein Mioke.
-
Task executor and result reciever.
Any Object can be a task executor or a result reciever by conform the protocols of
sender
andreciever
. A task can be any operation that you want be done in background thread. And callbacks will running in main thread again to do the things later.// self is any thing conform to protocol<sender, reciever> func someAction() -> Void { self.doTask({ () -> receiveDataType in return someCalculationNeedLongTime() }, identifier: "calculation") } // callback override func finishTaskWithReuslt(result: receiveDataType, identifier: String) { if identifier == "calculation" { Log.debugPrint(result) } } override func taskCancelledWithError(error: ErrorResultType, identifier: String) { super.taskCancelledWithError(error, identifier: identifier) }
-
Server
Provide some basic functionality of a server like onlieURL, offlineURL, isOnline etc. In test Mode, offline the server.
#if DEBUG Server.online = false #endif
-
ApiManager
Now you can manager request with
ApiManager
, just sublass fromBaseApiManager
and conform to protocolApiInfoProtocol
. Only need to provide some infomation about the API and set where the callback is, you are already finished the configuration of an API.var apiVersion: String { get { return "v2" } } var apiName: String { get { return "user/login" } } var server: Server { get { return mainServer } }
The BaseApiManager provide some basic method like:
func loadDataWithParams(params: [String: AnyObject]) -> Void
Setting delegate for receiving origin data:
extension ViewController: ApiCallbackProtocol { func ApiManager(apiManager: BaseApiManager, finishWithOriginData data: AnyObject) { if let apiManager = apiManager as? ApiLogin { print("login success: \n \(apiManager.originData())") } } func ApiManager(apimanager: BaseApiManager, failedWithError error: NSError) { if apiManager is ApiLogin { Log.debugPrint("login failed with error: \(error)") } } }
-
Attentions
- The request is generated by
KMRequestGenerator
, using Alamofire Request. - Customize your own way about "tell the request is succeed or not" in
BaseApiManager
, when your server returns error code or error message. And writing general solution inNetworkManager.dealError(error: ErrorType)
- The request is generated by
-
Database
Like ApiManager, only need to subclass from
KMPersistanceDatabase
and conform toDatabaseManagerProtocol
, providepath
,databaseName
,database
, you are already create a new database in your project. e.g.
class DefaultDatabase: KMPersistanceDatabase, DatabaseManagerProtocol {
override init() {
self.databaseName = "default.db"
self.path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! + "/" + self.databaseName
self.database = FMDatabaseQueue(path: self.path)
super.init()
}
}
-
Table and Record
Subclass from
KMPersistanceTable
and conform toTableProtocol
let you create a new table in a database. Any objcect conformRecordProtocol
can be record in the table you want. See more details in demo.Using this just like:
let table = UserTable() let newUser = UserModel(name: "Klein", uid: 310) table.replaceRecord(newUser)
-
Fetch
Fetch data with conditions using
DatabaseCommandCondition
:let table = UserTable() let condition = DatabaseCommandCondition() condition.whereConditions = "user_id >= 0" condition.orderBy = "user_name" let result = table.queryRecordWithSelect(nil, condition: condition)
-
Advanced
Always, database provide method of doing query or execute with sql directly, for complex database operation:
let db = DefaultDatabase() db.query("select * from tableDoesntExtist", withArgumentsInArray: nil)
- Using CocoaPods
- Custom extensions and categories
Not done yet~~~
- Networking:
cache, origin data transform to Model or View's data, priority of request. - Persistance: transform data to model or View's data after query.
- Animations
- Tools and Kits: TextKit like YYText, etc.
#License All the source code is published under the MIT license. See LICENSE file for details.