LiteNetwork is a lightweight and powerful network request framework written in Swift.
Chinese version of README -> README_cn
LiteNetwork is a lightweight network request framework based on the Apple native URLSession
API. It uses chain-sourceBag management system to ensure the orderly execution of multiple tasks, which means you can use method chaining to call multiple requests, and the requests will be sent in the order of invocation. You can easily and quickly change the configuration information, create and update tasks, perform unified management through the framework interface without caring about the underlying methods. The framework supports the creation and configuration of five tasks: data, download, uploadFile, uploadData and uploadStream. And provides a variety of custom interfaces.
- Handle URLSessionConfiguration and its update easily
- Support custom processing for data, download, upload and stream tasks
- Avoid inconveniently nested callbacks
- Multi-tasks asynchronous operations
- Unified tasks management
- Automatically invalidate session
- iOS 13.0+ / macOS 10.12+
- Swift 5.2+
Import the open source package to your project through the following operations:
Xcode
->File
->Swift Packages
->Add Package Dependcies
- search
https://github.com/lmyl/LiteNetwork
and add it to your targets
To integrate LiteNetwork into your project using cocoaPods, specify it in your Podfile
:
pod 'LiteNetwork'
- Initialize the configuration information and create the stream task use
makeStreamWith()
method:
// the default initialize create Default type of session
let token = LiteNetworkStream().makeStreamWith(host: "local", port: 9898)
- Make some custom changes here:
let token = LiteNetworkStream().makeStreamWith(host: "local", port: 9898)
.setEphemeralConfigureType() // Change Default to Ephemeral
.updateStreamReadCloseComplete {
print("Read closed")
}
- Call
startConnect()
orstartSecureConnect()
to resume the task:
let token = LiteNetworkStream().makeStreamWith(host: "local", port: 9898)
.setEphemeralConfigureType()
.updateStreamReadCloseComplete {
print("Read closed")
}
.startConnect()
- Connect with the server and process your operations. When there are multiple tasks, they will be executed asynchronously and serially.
let input = "hello world!"
token.readData(minLength: 1, maxLength: 1000, timeout: 30) { dataOrNil, eof, errorOrNil in
if let error = errorOrNil {
print("Error: " + error.localizedDescription)
}
if let data = dataOrNil {
print(String.init(data: data, encoding: .utf8)!)
}
print("EOF: \(eof)")
// return Bool to indicate whether current session will be invalidated
return false
}
// You can create multiple tasks in a row, just like this:
token.writeData(input: input.data(using: .utf8)!, timeout: 30, completionHandler: {
errorOrNil in
if let error = errorOrNil {
print("Error: " + error.localizedDescription)
} else {
print("Complete")
}
return false
})
token.simpleCommunicateWithSever(input: input.data(using: .utf8)!) { dataOrNil, errorOrNil in
if let data = dataOrNil {
print(String.init(data: data, encoding: .utf8)!)
}
if let error = errorOrNil {
print("Error: " + error.localizedDescription)
}
return false
}
- Notice if
- you close the read and write stream manually
- one operation(
writeData()
,readData()
orsimpleCommunicateWithSever()
) occurs anerror
- one operation(
writeData()
,readData()
orsimpleCommunicateWithSever()
) returntrue
in the completion handler
then session will call invalidateAndCancel()
and invalid itself automatically, the rest of operations will not be carried out.
// manual operations:
token.closeWriteStream()
token.closeReadStream()
// or
token.cancelSessionFinishCurrentTask()
// or
token.cancelSessionRightWay()
The basic steps are similar to the above. Notice that you need to call fire()
to resume all the tasks, rather than startConnect()
.
- Initialize and create your task:
let token2 = LiteNetwork().makeDataRequest(for: {
URLRequest(url: URL(string: "https://www.baidu.com")!)
})
- Make some custom changes:
let token2 = LiteNetwork().makeDataRequest(for: {
URLRequest(url: URL(string: "https://www.baidu.com")!)
})
.setRequestCachePolicy(for: .reloadIgnoringCacheData)
- Handle your data received from server(or other kinds of data)
.processData(for: {
response, dataOrNil in
if let data = dataOrNil, let string = String(data: data, encoding: .utf8) {
print(string)
}
})
- Resume all the tasks use
fire()
. You can create multiple tasks in a row, just like the codes below:
let token2 = LiteNetwork()
// first task
.makeDataRequest(for: {
URLRequest(url: URL(string: "https://www.baidu.com")!)
}).setRequestCachePolicy(for: .reloadIgnoringCacheData).processData(for: {
response, dataOrNil in
if let data = dataOrNil, let string = String(data: data, encoding: .utf8) {
print(string)
}
})
// second task
.makeDataRequest(for: {
return URLRequest(url: URL(string: "https://www.apple.com/cn/")!)
}).processData(for: {
response, dataOrNil in
if let data = dataOrNil, let string = String(data: data, encoding: .utf8) {
print(string)
}
}).processGlobeFailure(for: {
print("Error:" + $0.localizedDescription)
})
// resume all
.fire()
- And you can invalid your session at any times using the method below. if you have not called invalid method manually, the framework defaults to invalid the session after all tasks finished.
token2.cancelSessionFinishCurrentTask()
// or
token2.cancelSessionRightWay()
For more usages, please read LiteNetwork.swift
and LiteNetworkStream.swift
GitHub issue tracker: issue tracker ( report bug here )
Google email: 1269458422ly@gmail.com
or hxh0804@gmail.com
( if you have any questions or suggestions, contact us)