Skip to content

heji233/gitskills

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

#LiteNetwork

LiteNetwork is a lightweight and powerful network request framework written in Swift.

Overview

LiteNetwork is a lightweight network request framework based on the Apple native URLSession API. 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.

Features

  • Handle URLSessionConfiguration and its update easily
  • Support custom processing for data, download, upload and stream tasks
  • Specially set read, write and communication operations for stream tasks
  • Avoid inconveniently nested callbacks
  • Multi-tasks asynchronous operations
  • Unified tasks management
  • Invalid session after all tasks finished

Requirements

  • iOS 13.0+ / macOS 10.12+
  • Swift 5.2+

Installation

Swift Packages

Import the open source package to your project through the following operations:

  1. Xcode -> File -> Swift Packages -> Add Package Dependcies
  2. search https://github.com/lmyl/LiteNetwork and add it to your targets

Usage

Handle stream task

  1. 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)
  1. Make some custom changes here:
let token = LiteNetworkStream().makeStreamWith(host: "local", port: 9898)
            .setEphemeralConfigureType() // Change Default to Ephemeral
            .updateStreamReadCloseComplete {
                print("Read closed")
        }
  1. Call startConnect() or startSecureConnect() to resume the task:
let token = LiteNetworkStream().makeStreamWith(host: "local", port: 9898)
            .setEphemeralConfigureType()
            .updateStreamReadCloseComplete {
                print("Read closed")
        }
        .startConnect()
  1. Connect with the server and process your operations:
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: {
            errorrNil in
            if let error = errorrNil {
                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
        }
        
  1. Close the read and write stream, then session will call invalidateAndCancel() and invalid itself automatically; Notice if one operation occurs an error, or return true, the session will be invalidated immediately, and the rest of operations will not be carried out.
token.closeWriteStream()
token.closeReadStream()
// or 
token.cancelSessionFinishCurrentTask()
// or 
token.cancelSessionRightWay()

Handle data/upload/download task

The basic steps are similar to the above. Notice that you need to call fire() to resume all the tasks, rather than startConnect().

  1. Initialize and create your task:
let token2 = LiteNetwork().makeDataRequest(for: {
            URLRequest(url: URL(string: "https://www.baidu.com")!)
            })
  1. Make some custom changes:
let token2 = LiteNetwork().makeDataRequest(for: {
            URLRequest(url: URL(string: "https://www.baidu.com")!)
            })
            .setRequestCachePolicy(for: .reloadIgnoringCacheData)
  1. 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)
                }
            })
  1. 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/")!)
            }).setRequestCachePolicy(for: .reloadIgnoringCacheData).processData(for: {
                response, dataOrNil in
                if let data = dataOrNil, let string = String(data: data, encoding: .utf8) {
                    print(string)
                }
                expection.fulfill()
            }).processGlobeFailure(for: {
                print("Error:" + $0.localizedDescription)
                expection.fulfill()
            })
            // resume all
            .fire()
  1. 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

Contact

GitHub issue tracker: issue tracker (report bug here)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published